issues with maximum and average value in functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Theadmin77
    New Member
    • Nov 2006
    • 19

    issues with maximum and average value in functions

    Well ...this is a real challenge .....i got everything else working OK...but ...

    I have to get the average and maximum value out of a group of people thru two functions .I have problems passing value to function and processing it after is there ...
    Any help will be very appreciated.

    This is my code :


    #include <iostream>
    #include <cstdlib>
    using namespace std;

    //functions prototypes

    float averagegrade(St udent[],int)
    int maxgradestudent (Student [],int)


    //classes declaration

    class Person
    {
    public:
    Person(void);
    ~Person(void);
    void Setfirstname(ch ar []);
    char * Getfirstname(vo id);
    void Setlastname(cha r []);
    char * Getlastname(voi d);

    private:
    char Firstname[30];
    char Lastname [30];
    };

    class Student : public Person
    {
    public:
    Student(void);
    ~Student(void);
    void Setnid(char[]);
    char * getNID(void);
    void SetGrade(float) ;
    float GetGrade(void);

    private:
    float grade;
    char NID [30];


    };

    void main()
    {
    Student studentInfo[100];
    float studentGrade;
    int i,numStudents;
    char firstName[30];
    char lastName[30];
    char NID [30];
    float ave =0.0;
    int maxgrade;

    //check for errors

    do
    {
    cout << "Enter the number of students:";
    cin >> numStudents;
    if ((numStudents < 1) || (numStudents > 100))
    {
    cout << "Incorrect value. Try again...\n";
    }
    } while ((numStudents < 1) || (numStudents > 100));

    //Enter info for students:

    for (i=0;i<numStude nts;i++)
    {
    cout << "Student #"<<i+1 << endl;
    cout << " Enter the Student's first name :";
    cin >> firstName;

    cout << " Enter the Student's last name :";
    cin >> lastName;

    cout << "Enter the student's nid:";
    cin >> NID;


    cout << "Enter the student's grade:";
    cin >> studentGrade;

    //send data to functions

    studentInfo[i].Setfirstname(f irstName);
    studentInfo[i].Setlastname(la stName);
    studentInfo[i].Setnid(NID);
    studentInfo[i].SetGrade(stude ntGrade);

    //average call fuction (i dont know if is correct):

    ave= averagegrade(st udentInfo[i],ave);

    //maximum call function :

    maxgrade= maxgradestudent (studentInfo[i],maxgrade);
    }

    cout << "\n\n";

    for (i=0;i<numStude nts;i++)
    {
    cout << "The average grade is " << studentInfo[i].GetGrade() << endl;

    cout << "The student with the highest grade is :" << studentInfo[i].Getfirstname() <<" ";
    cout << studentInfo[i].Getlastname()< < "NID:"<< studentInfo[i].getNID()<<"gra de:"<<studentIn fo[i].GetGrade()<<"\ n";


    }

    //functions

    //constructor
    }

    Person::Person( void)
    {
    strcpy(Firstnam e,"");
    }

    //destructor

    Person::~Person (void)
    {
    strcpy(Firstnam e,"");
    }

    //function for first name

    void Person::Setfirs tname(char firstname[])
    {
    strcpy(Firstnam e,firstname);
    }

    //function for Last name

    void Person::Setlast name(char lastname[])
    {
    strcpy(Lastname ,lastname);
    }

    char * Person::Getfirs tname(void)
    {
    return Firstname;
    }

    char * Person::Getlast name(void)
    {
    return Lastname;
    }
    //nid function

    Student::Studen t(void)
    {
    strcpy (NID,"");
    }

    Student::~Stude nt(void)
    {
    strcpy (NID,"");
    }

    void Student::Setnid (char Nid[])
    {
    strcpy (NID,Nid);
    }

    char * Student::getNID (void)
    {
    return NID;
    }


    //function for grade:

    void Student::SetGra de(float studentGrade)
    {
    grade = studentGrade;
    }

    float Student::GetGra de(void)
    {
    return grade;
    }

    //function for average grade:

    ?
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Hello,
    I think the problem is that you are passing the same value that you try to store the result that is in
    Code:
     ave= averagegrade(studentInfo,ave);
    you pass ave (at the time 0.0) into the function and ask for ave in return. This isn't really a problem, except passing ave in serves no purpose.
    Secondly, the only way to pass anything other than a primitive (the basic types) is to pass reference to the object. That is, c doesn't (really) understand Objects. You have to pass a reference to the object and guarantee c you know what to do with it.
    This is sort of irrelevant, because the way it is represented here you won't REALLY notice it, but be aware all you pass in the method call is a reference to where the array starts (more information try the tutorials here or search for "pointers c" on the internet)

    While there are more than one ways to skin a cat, I would suggest you keep the same principle for your function protoypes, but use the second (int) variable to pass the size of the array (some will say this is pointless, but you have already calculated it and it is easier to use something you know without having to use new functions).

    The prototype, then, should be something like:
    Code:
    float averageGrade(int studentInfo[], int size)
    ;

    Remember that the Array i smerely a representation, I'll try to keep it as such, but it pays to understand how c is dealing with this structure (because if you pass anything more complicated you have to understand that you are only passing a pointer). I'll again suggest you visit the pointer idea for a while.

    Now we know we have a pointer to an integer array, and we have the size so we can try to implement:

    Code:
    float averageGrade(int studentScores[], int size)
    {
      int total = 0; //variable to keep sum of all elements in array
      for(int i=0; i<size; i++)
      {
        total=total+studentScores[i]; //add this students score to the total
      }
    
     return  (float)total/size; 
    }
    the "return (float)" makes sure that we return a number with decimal places. C (quite reasonably, I think) says : an integer divided by an integer must be an integer (because after all we have been dealing with INTEGERS), so we CAST the result into a float (floating point number) so that c remembers to give us more than that (that's probably not clear, but i can clarify that later if you need).

    you can implement maxgrade similarly, using the second int as array size. this timeyou will need a temporary variable (initialized at 0), and rather than adding to total, you will need to check whether the current item is bigger than the item in your temporary variable, and update the temporary varibale if it isn't.
    You won't have to worry about casting either, as this time you only have to return the maximum value (which is an int)

    Comment

    Working...