[using nested for loop] - Computers



Search Tech-Forums - link takes you to our Forum's search page.

Note: The following is only a text archive!


To view the actual forum discussion, please visit our website at http://www.tech-forums.net

Pages:1



using nested for loop

(Click here to view the original thread with full colors/images)



Posted by: mohsh2002

i have to wirite a C program that display the average of some test results for four experiments, each experiment has test results. They are giving me the results . i have to use nested for loop ..

this is what i did so far

#include<stdio.h>

int main()

{

float total,exp1,exp2,exp3,exp4,avg,counter;

counter = 0;
total = 0;
exp1=23.2+31.5+16.9+27.5+25.4+28.6;
exp2=34.8+45.2+27.9+36.8+33.4+39.4;
exp3= 19.4+16.8+10.2+20.8+18.9+13.4;
exp4=36.9+39.5+49.2+45.1+42.7+50.6;

for ( counter = 1 ; counter <= 4 ; ++counter );
{

i dunno what to do after this part?????
for (
avg=total/6;


printf("the total for i is %f and the avg is %f",i,avg);
}

return 0;

}



please help me..



Posted by: horndude

use first loop for iterations of each batch of results

use second loop to run thru the data and generate a cumulative total to be divided by number of iterations to calculate an average



Posted by: anandgagrawal

The solution i think is that u use an array to store the exps
i.e

float exp[3]
exp[0]=23.4+....
exp[1]=32.44+...
and so on
then in the for loop (u need to usse only 1)
u simply write
for(i=0;i<4;i++)
{
exp[i]=exp[i]/6
Printf("The avg for exp %d is %f",i,exp[i]);
}
Thats all



Posted by: mohsh2002

look what i have done

#include<stdio.h>

int main()

{
int i , j , results;
float total , avg , data;

for ( i= 1 ; i<=4 ; ++i )

{

printf("Enter the number of results for experiment #%d: ",i);
scanf("%d",&results);
printf("Enter %d for experiment #%d: ",results,i);



for (j=1,total= 0.0; j<=results;++j)

{

scanf("%f",&data);
total += data;

}

avg=total/results;

printf(" The average for experiment #%d is %.2f\n\n",i,avg);

}
system("PAUSE");
return 0;

}


but now i am the one who enters the value and i dont want that. we are not suppose to use strings or anything. YOU HAVE TO USE NESTED LOOP. thanx



Posted by: gab00n

Maybe you should copy exactly what your homework assignment says because from what i have read there is no need to use a nested loop. You shouldn't have any trouble at all with this stuff it is really easy, just think a little and write down a flowchart or pseudocode before you start to write your program. It will seem much easier then.





vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited.


PPC Management
vB Easy Archive Final - Created by Xenon