### How can I return the highest and second highest scores in this C++ program?

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.

 ``123456789101112131415161718192021`` ``````#include #include 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:

 ``123456789101112131415161718`` ``````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]; } } }``````

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 need to get:

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

Do I need to change some lines of code? Do I need to fix the for loop in my solution?
Last edited on
I can't see how your algorithm in getTopTwoScores is meant to work.

You go round the loop until you've been round more that "highest" times, where "highest" is the value of scores[1] or scores[0]. What does the value of scores[1] or scores[0] have to do with going round the loop? Where in your code do you actually compare score values to see what the highest score is? It doesn't seem to make any sense. What's it meant to do?
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;
}
}
}

Here is my output:

Input: 2 2 1
Output: 2 2

Here is the expected output:

Input: 2 2 1
Output: 2 1

Is my solution correct or not?
You should improve your code executing.
Suppose your inputs have 3 numbers and all those numbers will be checked to get the highest and the second highest value. Now, I try running this program : (2 2 1)
highest = secondhighest = 0;

Variable i = 0 (Score[0] = 2)
if(Score[0] > highest) -> if(2 > 0) correct
Do : highest = Score[0] = 2

if (Score[0] <= highest)
(2 <= 2) // yes
And : if(Score[0] > secondHighest)
(2 > 0) // yes
Finally do : secondHighest = Score[0] = 2;

CHECK : highest == secondHighest == 2 (2 == 2 == 2) -> !?!?!??!!?!?!!?!!?!?!!?!?!?!?!!?!?!?!!?!?!???!???!?!?!?!???!?!??!?!??!?!!????!?!??!??!?!?!?!?!???!??!?!?!?????

Have you figured out?
(Note : This is a secret, I can't say where the error is :))
Review and try finding the error, and hope all goes well :D

Will this help in what you are trying to achieve?
 ``1234567891011121314`` `````` highest = scores [1]; secondHighest = scores[1]; for (int i=0; i secondHighest) secondHighest= scores[i]; if(scores[i]

Hint : You want Highest > SecondHighest or Highest = SecondHighest?????
 ``12345678`` ``````void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest){ highest = secondHighest = 0; for (int i = 0; i < numScores; i++){ ////////////////////////////////////////////////////////////////////// if(scores[i] >= highest)highest = scores[i]; else if(scores[i] > secondHighest)secondHighest = scores[i]; ////////////////////////////////////////////////////////////////////// }}``````

Just simple.......
The second highest will always lower than the highest one. (You want this, right?)
`if(scores[i] >= highest)highest = scores[i];`
Just use Max finding algorithm...
`else if(scores[i] > secondHighest)secondHighest = scores[i];`
If this (value < highest), then the value has a chance to become to the second highest value.
Just use similar Max finding algorithm...

And similar (else if) , 3rd, 4th. 5th (layered)... :)
Last edited on
I modified my code, but I don't know if I am getting any closer.

 ``123456789101112131415161718192021222324252627`` ``````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]; } } }``````
Last edited on
Topic archived. No new replies allowed.