I am having problems with what to put in the script to make it happen.
Here is my current AverageGrade function:
float AverageGrade(Student[], int nStudents)
{
int i=0;
float sumofgrades=0;
float ave;
ave = sumofgrades/nStudents;
return ave;
i++;
};
Here is the for loop in the main function that I want it to go to:
for (i=0;i<numStudents;i++)
{
cout << "The average grade is " << AverageGrade << endl;
}
I ran it and I got back the same numbers as an average for each student
instead of one number as an average.
I tried the for loop first but I kept getting the "Missing ; before )
error"
I did everythink I knew of to fix it, but it didn't work.
Just incase, here is the entire script that I have right now.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Person
{
protected:
char m_FirstName[30];
char m_LastName[30];
public:
void SetFirstName(char []);
char * GetFirstName(void);
void SetLastName(char []);
char * GetLastName(void);
};
class Student : public Person
{
protected:
char m_NID[30];
float m_Grade;
public:
void SetNID(char []);
char * GetNID(void);
void SetGrade(float);
float GetGrade(void);
};
float AverageGrade(Student[], int nStudents);
void main()
{
Student studentInfo[100];
float studentGrade;
int i,numStudents;
char firstName[30], lastName[30], nid[30];
cout << "Enter the number of Students: ";
cin >> numStudents;
for (i=0;i<numStudents;i++)
{
cout << "\nStudent #" << i+1 << endl;
cout << " First name: ";
cin >> firstName;
cout << " Last name: ";
cin >> lastName;
cout << " NID: ";
cin >> nid;
cout << " Grade: ";
cin >> studentGrade;
studentInfo[i].SetFirstName(firstName);
studentInfo[i].SetLastName(lastName);
studentInfo[i].SetNID(nid);
studentInfo[i].SetGrade(studentGrade);
}
for (i=0;i<numStudents;i++)
{
cout << "The average grade is " << AverageGrade << endl;
}
system("PAUSE");
}
float AverageGrade(Student[], int nStudents)
{
int i=0;
float sumofgrades=0;
float ave;
ave = sumofgrades/nStudents;
return ave;
i++;
};
void Person::SetFirstName(char firstName[])
{
strcpy_s(m_FirstName,firstName);
}
char * Person::GetFirstName(void)
{
return m_FirstName;
}
void Person::SetLastName(char lastName[])
{
strcpy_s(m_LastName,lastName);
}
char * Person::GetLastName(void)
{
return m_LastName;
}
void Student::SetNID(char nid[])
{
strcpy_s(m_NID,nid);
}
char * Student::GetNID(void)
{
return m_NID;
}
void Student::SetGrade(float studentGrade)
{
m_Grade = studentGrade;
}
float Student::GetGrade(void)
{
return m_Grade;
}
Chris Varner, MCSA, MCDST
Network Technician
Clairson Plastics
2811 NE 14th St, Ocala, FL. 34470
Phone: (352) 732-3244 x120
Fax: (352) 368-1796
Cell: (352) 427-3555
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
http://www.clairson.com <http://www.clairson.com/>
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf
Of Jim Dougherty
Sent: Wednesday, April 23, 2008 4:30 PM
To: [email protected]
Subject: Re: [c-prog] Please Help
[email protected] <mailto:c-prog%40yahoogroups.com> 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:c-prog%40yahoogroups.com> [mailto:[
> mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED]
<mailto:c-prog%40yahoogroups.com> ] On Behalf
> Of Jim Dougherty
> Sent: Wednesday, April 23, 2008 2:10 PM
> To: [ mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED]
<mailto:c-prog%40yahoogroups.com>
> Subject: Re: [c-prog] Please Help
>
> [ mailto:c-prog%40yahoogroups.com [EMAIL PROTECTED]
<mailto:c-prog%40yahoogroups.com> 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.
>
[Non-text portions of this message have been removed]