Enter five scores and display the lowest score and the difference of each score from the highest score.

I'm so confused. I don't know if i'll use array or looping. Need you help. Thanks

Dani AI

Generated

This problem is best solved by storing the five scores and finding the lowest and highest once, then computing each difference as (highest − score). asked whether to use arrays or loops; and are correct that an array (or vector) plus a loop is the natural choice. The example below shows a concise, modern C++ style: read five integers into a fixed-size container, use std::minmax_element to get both extremes, then print the lowest and the differences.

#include <iostream>
#include <array>
#include <algorithm>

int main() {
    std::array<int,5> scores;
    std::cout << "Enter 5 scores:\n";
    for (int i = 0; i < 5; ++i) {
        if (!(std::cin >> scores[i])) return 1; // simple input check
    }

    auto mm = std::minmax_element(scores.begin(), scores.end());
    int low = *mm.first;
    int high = *mm.second;

    std::cout << "\nLowest score: " << low << "\n";
    std::cout << "Differences from highest (" << high << "):\n";
    for (int i = 0; i < 5; ++i)
        std::cout << "score[" << i << "] = " << scores[i]
                  << " -> " << (high - scores[i]) << '\n';

    return 0;
}

Notes and troubleshooting: using std::minmax_element does the min/max scan safely in one pass after input; alternatively, track low/high while reading to avoid a second traversal (an approach mentioned by ). Avoid Windows-only calls like system("pause>0") — prefer a portable pause (e.g., waiting for Enter) when needed. If scores can be fractional, use double instead of int. Handle bad input by clearing cin and re-prompting or rejecting the run, depending on desired behavior. Duplicate highest values produce zero differences, which is expected.

Recommended Answers

All 4 Replies

You need a loop to collect the scores, a variable to store the highest value found, and an array to store them in. Then another loop to find the difference between each value and the highest value that you stored in the variable in the first loop.

You have to write the code. We don't do your homework for you.

Enter five scores and display the lowest score
and the difference of each score from the highest score.

You will need two loops and an array to hold the 5 scores.

In the first loop, you can take in the 5 scores into the array and also find the lowest and highest score.

Hint ... set the 1st score that you take in to be both the lowest and highest score ...

Then for the next 4 scores, compare each next score to lowest and highest and update lowest and highest as needed.

So now you can print out the lowest and highest scores ...

On second loop though the array of scores... you can find and print out the difference of each value from the 'highest value' that you found above.

#include<iostream>
using namespace std;

int main()
{
int myArray[5];
int i;
int lowest;


for ( i=0; i<5; i++)
{

cout<< "Scores[" << i << "]:";
cin >> myArray[i];
}

lowest=myArray[0];


for (i =0; i<5; i++)
{
if(myArray[i]<lowest)
{
lowest=myArray[i];
}
}

cout << "\nThe Lowest numbers is " << lowest << endl;

system("pause>0");
return 0;
}

This is my program but how can i subtract the highest score to the other scores?

You could try something like this (that also validates input) ...

int main()
{
    int ary[5] = {0}; // get 'array' to hold 5 ints ...
    int low, high;

    // take in loop ...
    for( int i = 0 ; i < 5 ;  )
    {
        cout << "Enter integer # " << (i+1)  << " (of 5) "<< ": ";
        if( cin >> ary[i] )
        {
            if( i == 0 ) low = high = ary[0];
            else
            {

                if( ary[i] < low ) low = ary[i];
                else if( ary[i] > high ) high = ary[i];
            }
            ++i ;
        }
        else
        {
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
            cout << "\nERROR! Only integers are valid input here ...\n\n";
        }
    }

    cout << "\nLow is  :  " << low << endl;
    cout << "\nHigh is :  " << high << endl;


    // ...


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.