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

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.