Jump to content

[HELP]C++ - Structs and Arrays

infernowheels

The problem goes like this:

"Write a program that reads five student's names followed by their ten test scores. The programs should then output each student's name followed by the test scores and the relevant average score. The program must then display if the sutdent is pass or fail (average score is 75.00). The program must also display the name and average of student with the highest and lowest average."

 

I'm done with most of the problem, however I can't figure out how to get the highest and lowest average together with the name of the student..

(referring to the final loop down to the last lines)

 

Here's my code:

#include<iostream>using namespace std;struct student{       char name[50];       int grade[10];       int total;       int ave;};int main(){      student p[5];      int highest, lowest;      for(int a = 0; a < 5; a++)      {      cout << "Enter Full Name: ";      cin >> p[a].name;      p[a].total = 0;          for(int x = 0; x < 10; x++)          {                  cout << "Enter a score: ";                  cin >> p[a].grade[x];                                    p[a].total += p[a].grade[x];          }      p[a].ave = (p[a].total/10);      cout << endl;      }            cout << endl;            for(int b = 0; b < 5; b++)      {      cout << "Student's name: " << p[b].name << endl;      cout << "Scores: " << endl;           for(int y = 0; y < 10; y++)           {                   cout << p[b].grade[y] << endl;           }      cout << "The student's average is: " << p[b].ave << endl;           if(p[b].ave > 74)           {                cout << "Congratulations, you have passed!" << endl;                cout << endl;           }            else            {                 cout << "You have failed." << endl;                 cout << endl;            }      }                  for(int c = 0; c < 5; c++)      {              if(p[c].ave > p[c-1].ave)              {                          highest = p[c].ave;              }              else              {                          highest = p[c-1].ave;              }                  if(p[c].ave < p[c-1].ave)                  {                              lowest = p[c].ave;                  }                  else                  {                              lowest = p[c-1].ave;                  }      }            cout << "The student with the highest average is: " << p[0].name << " with an average of: " << highest << endl;      cout << "The student with the lowest average is: " << p[0].name << " with an average of: " << lowest << endl;      system("pause");return 0;}      

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

I would just write a function for it and pass it the list of students....

 

Alternatively you could write a function to sort the students.

 

You could have the list be self sorting as you add them.

 

Also, I would remove the hardcoded number 5 and replace it with a constant like numStudents

Link to comment
Share on other sites

Link to post
Share on other sites

What do you mean when you say get the highest and lowest average together? Like add them, sort them..?

Nope, I meant find the students with the highest average and the lowest average.

Both their name and average grade should be outputted.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

I would just write a function for it and pass it the list of students....

Actually, there are additional instructions pertaining to doing that, but I just want to figure out how to do this first before splitting them out into functions.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

I would just write a function to find the average.

 

Use that function inside a new function to find the min/max student....

 

Something like:

double getAverageGrade(student[] s) {
    double avg = 0;
    for(int i=0; i<10; i++)
        avg+=s.grade;
    avg /= 10;
    return avg;
}

student maxStudent(student[] s) {
    student max;
    for(int i=0;i<5;i++)
        if(getAverageGrade(max)<getAverageGrade(s))
            max = s
    return max;
}

student minStudent(student[] s) {
    student min;
    for(int i=0;i<5;i++)
        if(getAverageGrade(min)>getAverageGrade(s))
            min = s
    return min;
}

etc
etc

 

Note: Hardcoding all these magic numbers is generally considered a bad thing. Then again implementing this as a list of structs is also pretty bad.... :P

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of storing the values of the highest and lowest averages, store the indices of the students with the highest and lowest averages. Then you can look up their info in array "p".

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of storing the values of the highest and lowest averages, store the indices of the students with the highest and lowest averages. Then you can look up their info in array "p".

 

This is a great idea with little modification needed.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×