Click to See Complete Forum and Search --> : newbie question
AA225
January 27th, 2005, 01:22 PM
im just practicing some basic programming and have a question..
My program calculates a students average based on the a user-assigned number of assignments. my question is how do i make the program get the sum all the values that are put in during each repetition of the 'while' loop?
Modeller
January 29th, 2005, 08:45 AM
It might be difficult to provide an exact answer to your question since how the student's values are given is unclear. However, I might suggest the following:
double sum = 0.0;
int numberOfItems = 0;
while (more to go) // Here specify how the loop will stop
{
// Read the studen's value
float value = student's value; // Here place your input-reading
sum += value;
numberOfItems++;
}
float average = sum / (numberOfItems + 1);
// if your data requires storage then use an array
// float value __gc[] = new float __gc [numberOfValues]
I hope this will help!
cilu
January 29th, 2005, 03:27 PM
Why
float average = sum / (numberOfItems + 1);
and not
float average = sum / numberOfItems;
If you enter the while loop then the first one is wrong. If you did it to avoid division by 0, then you should have done something like:
float average = 0;
if(numberOfItems>0) average = sum / numberOfItems;
or
float average = sum;
if(numberOfItems>0) average /= numberOfItems;
AA225
January 29th, 2005, 08:18 PM
got it working....thanks for your help everyone :)
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.