- #1
SN1987a
- 35
- 0
Hi guys, I'm really new to computer programming, and I've been sortof thrown into it without much introduction. This is a short C program I have to write for one of my physics courses.
To biggin with, the program has to prompt the user for a bunch of real numbers, which are to be stored in an array and to be used for computations later. My problem is at the very beginning. I'm thinking of using scanf to read the input and assigning it to a slot in the array. I would do something like this:
int i;
float data[10];
for(i=0;i<10;++i)
scanf("%f", &data);
This seems to work, but for some reason, my professor asks me to use a while loop here. Why and most importantly how would I get a while loop to do the job?
I'm not even sure I understand how a while loop works. So we have that while certain codition is satisfied, then something happens (or there can be multiple possibilities with if/else statements). But I don't see how a while loop can help with gathering a bunch of data and dumping it into an array.
I really need help to get started on this. I'm pretty cofused about C, and most importantly I think, I know very little syntax.
To biggin with, the program has to prompt the user for a bunch of real numbers, which are to be stored in an array and to be used for computations later. My problem is at the very beginning. I'm thinking of using scanf to read the input and assigning it to a slot in the array. I would do something like this:
int i;
float data[10];
for(i=0;i<10;++i)
scanf("%f", &data);
This seems to work, but for some reason, my professor asks me to use a while loop here. Why and most importantly how would I get a while loop to do the job?
I'm not even sure I understand how a while loop works. So we have that while certain codition is satisfied, then something happens (or there can be multiple possibilities with if/else statements). But I don't see how a while loop can help with gathering a bunch of data and dumping it into an array.
I really need help to get started on this. I'm pretty cofused about C, and most importantly I think, I know very little syntax.