"for" loop to find the highest score in an array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lostncland
    New Member
    • Nov 2006
    • 2

    "for" loop to find the highest score in an array.

    I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense?


    /*Defines a global constant called N throughout the file.
    Note that N = 10.
    */

    #define N 10
    #include <iostream>
    using namespace std;

    /*Define a StudentScores class with methods called

    (1) getHighestScore – find the highest score in the array
    (2) getLowestScore – find the lowest score in the array
    (3) getAverageScore – compute the average score

    */
    class StudentScores
    {
    private:
    int _scores[N];
    public:
    StudentScores(i nt scores[])
    {
    for(int i = 0; i < N; i = i + 1)
    _scores[i] = scores[i];
    }

    int getHighestScore ()
    {
    int highest;
    highest = _scores[0];
    for (int i = 0; i < N; i++)
    if (a [i]> scores [0]
    highest = a [i]
    return highest;
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Originally posted by lostncland
    I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense?


    /*Defines a global constant called N throughout the file.
    Note that N = 10.
    */

    #define N 10
    #include <iostream>
    using namespace std;

    /*Define a StudentScores class with methods called

    (1) getHighestScore – find the highest score in the array
    (2) getLowestScore – find the lowest score in the array
    (3) getAverageScore – compute the average score

    */
    class StudentScores
    {
    private:
    int _scores[N];
    public:
    StudentScores(i nt scores[])
    {
    for(int i = 0; i < N; i = i + 1)
    _scores[i] = scores[i];
    }

    int getHighestScore ()
    {
    int highest;
    highest = _scores[0];
    for (int i = 0; i < N; i++)
    if (a [i]> scores [0]
    highest = a [i]
    return highest;
    Hi,

    The code is incomplete.

    Code:
    /*Defines a global constant called N throughout the file. 
    Note that N = 10.
    */
    
    #define N 10 
    #include <iostream>
    using namespace std;
    
    /*Define a StudentScores class with methods called
    
    (1) getHighestScore – find the nHighest score in the array
    (2) getLowestScore – find the lowest score in the array
    (3) getAverageScore – compute the average score
    
    */
    class StudentScores
    {
       private:
          int m_nScores[N];
       
       public:
          StudentScores(int a_nScores[])
          {
             for(int i = 0; i < N; i = i + 1)
             {
                m_nScores[i] = a_nScores[i];
             }
          }
    
          int GetHighestScore()
          {
             int 
                nHighest = 0;
    
             nHighest = m_nScores[0];
             for (int i = 0; i < N; i++)
             {
                if (a [i] > a_nScores [0])
                {
                   nHighest = a [i]; 
                }
             }
             return nHighest;
          }
    };
    Can you post your full code?

    Regards,
    M.Sivadhas.

    Comment

    • lostncland
      New Member
      • Nov 2006
      • 2

      #3
      Here is the full code:

      /*Defines a global constant called N throughout the file.
      Note that N = 10.
      */

      #define N 10
      #include <iostream>
      using namespace std;

      /*Define a StudentScores class with methods called

      (1) getHighestScore – find the highest score in the array
      (2) getLowestScore – find the lowest score in the array
      (3) getAverageScore – compute the average score

      */
      class StudentScores
      {
      private:
      int _scores[N];
      public:
      StudentScores(i nt scores[])
      {
      for(int i = 0; i < N; i = i + 1)
      _scores[i] = scores[i];
      }

      int getHighestScore ()

      int nHighest = 0;
      nHighest = m_nScores [0];
      for (int i = 0; i < N; i++)
      if (a [i] > a_nScores [0];
      return nHighest;
      {
      int getLowestScore( )
      {
      int lowest;
      lowest = _scores[0];
      I am supposed to here WRITE A 'while' LOOP THAT DETERMINES THE LOWEST SCORE
      return lowest;
      }

      double getAverageScore ()
      {
      double ave;
      I am supposed to here WRITE A 'for' LOOP THAT COMPUTES THE AVERAGE GRADE
      return ave;
      }
      };

      int main(){

      //Create a scores array and assign 10 scores
      int scores[] = {1,29,31,45,5,6 1,99,87,12,100} ;

      //Create a student scores object
      StudentScores studentOne(scor es);

      //Use the getHighestScore method to display the highest score
      cout << "The highest score is " <<
      studentOne.getH ighestScore() << endl;
      cout << "The lowest score is " <<
      studentOne.getL owestScore() << endl;
      cout << "The average score is " <<
      studentOne.getA verageScore() << endl;
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by lostncland
        Here is the full code:
        int getHighestScore ()

        int nHighest = 0;
        nHighest = m_nScores [0];
        for (int i = 0; i < N; i++)
        if (a [i] > a_nScores [0];
        return nHighest;
        {
        there are a few errors in the above function, the following should work
        Code:
        int getHighestScore()
        {                           // ** added {
        int nHighest = 0;
        nHighest = _scores [0];     // ** fixed name
        for (int i = 0; i < N; i++)
        if (_scores  [i] > nHighest) nHighest=_scores  [i];  // ** fixed test
        return nHighest;
        }                             // ** changed { to }
        make sure you are consistent with variable names

        now try and complete the rest of the program

        Comment

        Working...