Problem with nested loop
I just learned nested loop. And I understand only a little bit about it. So I try do some exercises. At the end I couldn't get the output below:
Experiment #1: 23.2 31.5 16.9 27.5 25.4 28.6
Average: 25.52
Experiment #2: 34.8 45.2 27.9 36.8 33.4 39.4
Average: 36.25
Experiment #3: 19.4 16.8 10.2 20.8 18.9 13.4
Average: 16.58
I also need to modify the program so that the no. of experiment and number of test results are entered by the user.
This is my source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <stdio.h>
int main()
{ int x,y,num=1,total=0;
float result,avrg;
for(x=1; x<=3; x++)
{printf("Experiment #%d:", x);
for(y=1; y<=6; y++)
scanf("%.1f", &result);
total=+result;
avrg=result/6;
printf("\n");
printf("Average: %.1f", avrg); }
system("pause");
return 0;}
|
Last edited on
your program executes this bit 6 times:
scanf("%.1f", &result);
then does this once in the inner loop 3 times altogether because of the outer loop:
1 2 3 4
|
total=+result;
avrg=result/6;
printf("\n");
printf("Average: %.1f", avrg);
|
Do you know why?
also this is how you loop 6 times:
for(y=0; y<6; y++)
this bit shouldn't be in the inner loop:
1 2
|
printf("\n");
printf("Average: %.1f", avrg);
|
IF you fix the inner loop it will print 18 times
I see. I will try fix the inner loop. Thanks for your advices
Topic archived. No new replies allowed.