[email protected] wrote: > OK I redid some stuff and everything seems to be working except for the > average and the highest grade portion. Here is what I have now. > Can you see what I need to do to get this working? > > > void main() > { > Student studentInfo[100]; > float studentGrade; > int i,numStudents; > char firstName[30], lastName[30], nid[30]; > > studentInfo[i].SetFirstName(firstName); > studentInfo[i].SetLastName(lastName); > studentInfo[i].SetNID(nid); > studentInfo[i].SetGrade(studentGrade); > } > > for (i=0;i<numStudents;i++) > { > cout << "Student " << studentInfo[i].GetFirstName(); > cout << "'s grade is " << studentInfo[i].GetGrade() << endl; > } > > system("PAUSE"); > } > > float AverageGrade(Student [], int value) > { > float sum = 0.0; > int NumofVals = 0; > > sum += value; > NumofVals++; > > return sum / NumofVals; > }; >
I might be missing the boat but I think that each student has one and only one grade and you want to find the average grade for all students and have the AverageGrade function return this value. If so: Redefine AverageGrade so that you pass the number of students in to it (I am not sure if that is what you meant by value). I would do it like this: float AverageGrade (Student [], int number_of_students) Within AverageGrade you want a for loop that iterates based upon the number of students. Within the loop, for each student you want to add that students grade to the sum. When done looping, divide the sum by the number of students and return that value to the caller. Are you having trouble understanding this approach or are you having trouble turning this algorithm into code? > > -----Original Message----- > From: [ mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED] [mailto:[ > mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED] On Behalf > Of Jim Dougherty > Sent: Wednesday, April 23, 2008 2:10 PM > To: [ mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED] > Subject: Re: [c-prog] Please Help > > [ mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED] wrote: >> I need to figure out what is wrong with this. I need the average of >> the scores to be calculated in the AverageGrade function, but I am >> having problems. Can someone help me? >> >> float AverageGrade(Student studentGrade, int average) >> { >> int i; >> float n; >> float AveSco = 0.0; >> bool validString = true; >> >> for(i=0;i<n;i++) >> { >> AveSco = AveSco + n; >> }; >> AveSco = AveSco / n; >> cout << "The average student grade is: " << AveSco << endl; >> return AverageGrade; >> }; >> > > You have a variable 'n' in main() that you set to the number of students > > via operator input (cin). > > You have another variable 'n' in your AverageGrade function that has the > > same name but it is not the same variable and you do not initialize it > in > any way. It's value is undefined. Perhaps what you want to do is make > 'n' > or 'number_of_students' and argument that is passed in to the > AverageGrade > function. Also it's type should be int not float. > > You have a 'for' loop in AverageGrade that is intended to calculate the > sum > of the scores for all students and you are keeping this sum in AveSco. > Just > to make thinks clearer, create a new float parameter called > sum_of_all_scores > and use it in your 'for' loop after initializing it to zero. During > each pass > thru your 'for' loop you need to increment sum_of_all_scores by the > current > students score. > > After you are done with the loop calculate the average score using the > sum and > the number of students. >
