Thread: Beginner. Help with displaying highest and lowest numbers

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22

    Beginner. Help with displaying highest and lowest numbers

    I need this code to also display the highest and lowest numbers inputted, but I'm not sure how to go about it.

    Code:
    #include <iostream>
    using namespace std;
    
    
    double score;
    int sum = 0;
    int average = 0;
    
    
    int main()
    {
    	cout << "We are going to display the grades for 10 test scores\n";
    
    
    	for (int z = 0; z < 10; z++)
    	{
    		cout << "Please enter a test score\n";
    		cin >> score;
    
    
    		sum += score;
    
    
    		if (score >= 90)
    		{
    			cout << "Your score is A\n";
    		}
    		else if (score >= 80)
    		{
    			cout << "Your score is B\n";
    		}
    		else if (score >= 70)
    		{
    			cout << "Your score is C\n";
    		}
    		else if (score >= 60)
    		{
    			cout << "Your score is D\n";
    		}
    		else
    		{
    			cout << "Your score is F\n";
    		}
    
    
    		
    
    
    	}
    
    
    	average = sum / 10;
    
    
    	cout << "\nThe average score was" << average << endl;
    
    
    
    
    
    
    
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    well in addition to calculate the sum, you need to calculate minimum score and maximum score (by comparing current score to the currently stored minimum and maximum)

    Ad print these values after the loop ends
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Start with thinking of how you would solve the problem of displaying the highest number input. Clearly, you must first determine what is the highest number input. How would you go about doing that?

    By the way, as I mentioned in your other thread, you should avoid global variables. Declare those variables in the main function instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    I tried this, but the highest and lowest number default to the last number the user entered.
    Code:
    int max = 0;
    	int min = 9999;
    
    
    	if (score < min) min = score;
    	cout << "The lowest score is " << min << endl;
    
    
    	if (score > max) max = score;
    	cout << "The highest score is " << max << endl;
    	
    
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have the right idea, but you need to define the variables before the loop and then print their final values after the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    I moved it to be defined before the loop, but how do i print the final values? It still defaults to the last number entered

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    Code:
    #include <iostream>
    using namespace std;
    
    
    double score;
    int sum = 0;
    int average = 0;
    int max = 0;
    int min = 9999;
    
    
    int main()
    {
    	
    
    
    	
    	cout << "We are going to display the grades for 10 test scores\n";
    
    
    	for (int z = 0; z < 10; z++)
    	{
    		cout << "Please enter a test score\n";
    		cin >> score;
    
    
    		sum += score;
    
    
    		if (score >= 90)
    		{
    			cout << "Your score is A\n";
    		}
    		else if (score >= 80)
    		{
    			cout << "Your score is B\n";
    		}
    		else if (score >= 70)
    		{
    			cout << "Your score is C\n";
    		}
    		else if (score >= 60)
    		{
    			cout << "Your score is D\n";
    		}
    		else
    		{
    			cout << "Your score is F\n";
    		}
    
    
    
    
    
    
    	}
    
    
    	average = sum / 10;
    
    
    	cout << "\nThe average score was" << average << endl;
    
    
    	if (score < min) min = score;
    	cout << "The lowest score is " << min << endl;
    
    
    
    
    	if (score > max) max = score;
    	cout << "The highest score is " << max << endl;
    
    
    
    
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is how I might write your program:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "We are going to display the grades for 10 test scores\n";
    
        int sum = 0;
        int max = 0;
        int min = 9999;
    
        for (int z = 0; z < 10; z++)
        {
            cout << "Please enter a test score\n";
            double score;
            cin >> score;
    
            sum += score;
    
            if (score < min)
            {
                min = score;
            }
            if (score > max)
            {
                max = score;
            }
    
            if (score >= 90)
            {
                cout << "Your score is A\n";
            }
            else if (score >= 80)
            {
                cout << "Your score is B\n";
            }
            else if (score >= 70)
            {
                cout << "Your score is C\n";
            }
            else if (score >= 60)
            {
                cout << "Your score is D\n";
            }
            else
            {
                cout << "Your score is F\n";
            }
        }
    
        int average = sum / 10;
    
        cout << "\nThe average score was" << average << endl;
        cout << "The lowest score is " << min << endl;
        cout << "The highest score is " << max << endl;
    }
    Notice that:
    • I moved your variable declarations into the main function. In fact, I followed the rule of thumb that variables should be declared near first use.
    • I moved the code that checks for score < min and score > max into the loop body. You need to do this comparison on every iteration otherwise you would only ever be checking the very last score entered with 0 and 9999.
    • I used blank lines to group parts of the code, but I did not overuse them.

    By the way, did you really intend for integer division when you wrote sum / 10, and is the average really supposed to be an int rather than a double?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    Yes, I purposely did sum / 10. I'm trying to find the average and the user can only enter ten numbers so I did the sum of all numbers divided by 10. And the average probably should have been a double now that you mention it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 01-29-2013, 09:47 PM
  2. Highest and lowest value
    By manolo in forum C Programming
    Replies: 1
    Last Post: 01-23-2010, 10:57 AM
  3. HELP: Exclude the highest and lowest number...
    By Ocin101 in forum C Programming
    Replies: 12
    Last Post: 07-21-2009, 01:51 AM
  4. Trying to sort numbers from lowest to highest
    By jw232 in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2008, 04:03 PM
  5. Displaying highest and lowest values
    By beastofhonor in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2006, 08:24 PM