Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-12-2005, 11:27 PM   #1 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 20

mohsh2002

Default using nested for loop

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..
mohsh2002 is offline  
Old 03-12-2005, 11:35 PM   #2 (permalink)
 
Master Techie

Join Date: Apr 2004

Posts: 2,534

horndude is on a distinguished road

Default

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
horndude is offline  
Old 03-13-2005, 01:27 PM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 3

anandgagrawal

Default

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
anandgagrawal is offline  
Old 03-13-2005, 04:08 PM   #4 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 20

mohsh2002

Default

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
mohsh2002 is offline  
Old 03-13-2005, 04:52 PM   #5 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

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.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On