0

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[1];
 secondHighest = scores[2];
 if(highest == secondHighest)
 {
 highest = scores[numScores];
 secondHighest = scores[numScores];
 }
 if(numScores == 1)
 {
 highest = scores[0];
 secondHighest = scores[0];
 }
 }

Here is my output:

Input: 9 45 76 87 65 87 45 76 67 74
Output: 76.00 87.00

Here is the expected output I am trying to get:

Input: 9 45 76 87 65 87 45 76 67 74
Output: 87.00 87.00

What lines of code do I need to change? Did I place the values incorrectly?

2
Contributors
7
Replies
10
Views
5 Years
Discussion Span
Last Post by andrew mendonca
0

Your getTopTwoScores() method simply picks the array elements 1 and 2 for the highest two scores (which are 76 and 87). You'll need to use a loop to find the highest scores, like you did to get the scores from input.

0

Is this what you meant?

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

Not exactly... you'll pretty well need to start that method from scratch. Very generally: you need to keep a record of the highest and second highest, and in each iteration you need to compare the element to the current highest and second highest and adjust appropriately.

0

Here is my updated solution:

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

        if(scores[i] <= highest && scores[i] > secondHighest)
        {
            secondHighest = scores[i];
            scores[1] = secondHighest;
        }
    }
}

This is my output:

Input: 2 2 1
Output: 2 2

This is the expected output:

Input: 2 2 1
Output: 2 1

Is my solution correct, or do I need to change something?

0

Not quite, but you're getting there. Keep in mind that if you have a value as the highest, and a new value is higher, the highest will become the second highest (your first if statement in the for-loop). You should also change your second if statement in the for-loop to an else if and remove the scores[i] <= highest expresion. This ensures that, at most, one is executed. One last thing, I don't see why you would need to assign the highest and second highest back to the array.

0

Am I getting closer? Is there something else I need to add/fix?

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

        else if(scores[i] > secondHighest)
        {
            secondHighest = scores[1];
        }
    }
}
0

Here is another modified solution:

void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest)
{
    highest = scores[0];
    int highestIndex;
    for(int i = 1; i < numScores; i++)
    {
        if(scores[i] > highest)
        {
            highest = scores[2];
        }
    }
    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];
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.