Define a getTopTwoScores() function with the specification and prototype shown below:

// Set highest to the score with highest value and secondHighest to the score with the next highest value.
// If there are two highest scores with the same value then set both highest and secondHighest to that value.
// If numScores is 1 then set both highest and secondHighest to scores[0]
void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest) ;

Here is the driver (main()) used to test my function.

#include <iostream>
#include <iomanip>
using namespace std;

const int MAX_SCORES = 10; // Maximum number of scores

void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest);

int main() {

double scores[MAX_SCORES];
int scoreCount;
double highestScore, secondHighestScore;
cin >> scoreCount;
scoreCount = min(scoreCount, MAX_SCORES);
for (int i = 0; i < scoreCount; i++)
    cin >> scores[i];
getTopTwoScores(scores, scoreCount, highestScore, secondHighestScore) ;
cout << highestScore << " " << secondHighestScore << endl;
return 0;
} 

Here is my solution:

void getTopTwoScores(double scores[], int numScores, double& highest, double&
secondHighest)
{
    highest = scores[0];
    int highestIndex = scores[0];
    for(int i = 1; i < numScores; i++)
    {
        if(scores[i] > highest)
        {
            highest = scores[i];
        }
    }
    if(highestIndex == 0)
    {
        secondHighest = scores[1];
    }
    else
    {
        secondHighest = scores[0];
    }
    for(int i = 1; i < numScores; i++)
    {
        if(i == highestIndex && scores[i] > secondHighest)
        {
            secondHighest = scores[i];
        }
    }
}

Here is my output:

Sample run 1:

Input: 67 86 55.5 46.4 90 87 71 59.5 -1
Output: 90.00 67.00

Sample run 2:

Input: 86 55.3 94.3 56 78 94.3 94.2 -1
Output: 94.30 86.00

Here is the expected output:

Sample run 1:

Input: 67 86 55.5 46.4 90 87 71 59.5 -1
Output: 90.00 87.00

Sample run 2:

Input: 86 55.3 94.3 56 78 94.3 94.2 -1
Output: 94.30 94.30

Is there something missing? Does anything need to be changed?

    void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest)
    {
        if (numScores == 1)
        {
            highest = secondHighest = scores[0];
            // nothing else to do
            return;
        }

        // initialize highest and secondHighest
        // assume highest is scores[0]
        // secondHighest we'll set to negative maximum double value
        highest = scores[0];
        secondHighest = - std::numeric_limits<double>::max();

        for(int i = 1; i < numScores; i++)
        {
            if(scores[i] > highest || scores[i] > secondHighest)
            {
                if (scores[i] > highest)
                {
                    secondHighest = highest;
                    highest = scores[i];
                }
                else
                {
                    secondHighest = scores[i];
                }
            }
        }
    }

Also in your main(), verify that scoreCount > 0 and you'll also need to flush the input stream to get rid of any new line chars.

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.