Thread: Calculating Average and high/lowest test score

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Question Calculating Average and high/lowest test score

    Hello, I am getting frustrated because I am currently trying to figure out how to solve this program:

    Write a program that prompts the user for test scores (doubles). The user enters -1 to stop the entry. After all of the test scores have been entered, calculate the average, the highest and the lowest test score.

    So far this is what I have:

    #include <iostream>

    using namespace std;

    int main()
    {
    double score, average, highest, lowest;
    int counter = -1;
    do
    {
    counter++;
    cout << "Please enter a score (enter -1 to stop): ";
    cin >> score[counter];
    } while (scores[counter] >= 0);


    cout << "Highest is ";
    cin >> highest;
    cout << "Lowest is ";
    cin >> lowest;

    // I am stuck at this point which is to CALCULATE AND DISPLAY THE AVERAGE,
    // THE HIGHEST AND LOWEST TEST SCORE


    }
    Last edited by aexi; 07-25-2010 at 01:48 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Hello, I am getting frustrated because I am currently trying to figure out how to solve this problem by writing a program:

    Write a program that prompts the user for test scores (doubles). The user enters -1 to stop the entry. After all of the test scores have been entered, calculate the average, the highest and the lowest test score.

    So far this is what I have:
    Code:
    #include <iostream>
    
    int main()
    {
       using namespace std;
       double score, average, highest, lowest;
       int counter = -1;
       do
       {
          counter++;
          cout << "Please enter a score (enter -1 to stop): ";
          cin >> score[counter];
       } while (scores[counter] >= 0);
    
       cout << "Highest is ";
       cin >> highest;
       cout << "Lowest is ";
       cin >> lowest;
    
       // I am stuck at this point. Here is what
       // I have tried but I don't have a good grasp
       // of how to use the necessary language features:
    
       //code that we can help you with
       // . . . 
       return 0;
    }
    BTW, do you want to ask the user what the highest and lowest are? Suppose the user enters some random value... Instead, you can calculate the max and min of the set without the user's help... how would you do it as a human?

    EDIT: And what does score[counter] mean? What is scores? Try to break this program down maybe a line at a time and see if it compiles and behaves as you expect.
    Last edited by CodeMonkey; 07-25-2010 at 03:19 PM. Reason: return 0;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    I'm pretty new to c++ and this class I am taking is going really fast for me which is why I'm having a hard time with these problems. I have tried to solve it again, and got what is listed below. However, it keeps giving me errors and I still do not understand how to get the highest/lowest scores that the user enters. From the scores that the user entered, I want the program to find out what the highest and lowest are. The score[counter] is the user's input of all the scores.


    PHP Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
        
    double scores[75];
        
    int counter = -1;
        do
        {
            
    counter++;
            
    cout << "Please enter a score (enter -1 to stop): ";
            
    cin >> scores[counter];
        } while (
    scores[counter] >= 0);
     
        
    double averagehighestlowest;
        
    int sum;
        
    sum 0;
        
    sum sum scores;
        
    average sum counter;
     
        
    cout << "Average is ";
        
    cin >> average;
        
    cout << "Highest is ";
        
    cin >> highest;
        
    cout << "Lowest is ";
        
    cin >> lowest;
     


  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    The code up until the end of the do-while loop makes sense to me. You accept up to 75 scores, where the user can enter any negative number to stop entry.

    Now you make a little mistake with your sum. Addition between a double (sum) and an array of doubles (scores) is not defined explicitly. You need to add each number to sum individually. That is, scores[0], scores[1], all the way to scores[counter-1].

    Then your cout calls at the end. You seem to want to display the average, but instead you ask the user to give a value for the average. It looks like a slip of the mind. You want this:
    Code:
    cout << "Average is " << average << endl;
    Which is the same as if you did
    Code:
    cout << "Average is ";
    cout << average << endl;
    The endl tells the stream to go to a new line and actually print out what you wrote right away.

    You also seem to make no effort to find what the highest and lowest are. How would you know? What makes a number the biggest or the smallest? What would your program have to do? I'll do the highest.
    Code:
       //make sure highest starts at 0; the lowest allowable value.
       double score, average, highest = 0, lowest;
       int counter = -1;
       do
       {
          counter++;
          cout << "Please enter a score (enter -1 to stop): ";
          cin >> score[counter];
          if(score[counter] > highest)
             highest = score[counter];
          //The lowest will be a tiny bit trickier.
       } while (scores[counter] >= 0);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    std::min, std::max, and std::accumulate would be the fancy C++ way of doing it, but I gather you need to learn the fundamentals of programming itself rather than how to use the C++ standard library.

    At the moment we can tell you have a lack of basic understanding of concepts as you're playing the syntax guessing game for a very basic assignment. I'm not prepared to give you enough rope to hang yourself on this one. Make more time to study how to use the language.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Tags for this Thread