### How can I obtain 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:

 ``12345678910111213141516171819202122232425262728`` ``````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 that needs to be add? Do any lines of code need to be changed?
Use Index is better. But do you know Max finding Algorithm? :)
There are many methods around, seperating the machine into two groups... Is the method you are using. Go to "My topic" and find your old duplicate post, copy my algorithm and post it here. It only uses two if-else if conditions in a line.

Make sure the second highest always lower than the highest value,
Last edited on
 ``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
Here is my updated and simplified solution:

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

Here is my output:

Input: 60 50 40 30 55 56 57 58 59 61 -1
Output: 61.00, 59.00

I want to get this expected output:

Input: 60 50 40 30 55 56 57 58 59 61 -1
Output: 61.00, 60.00

Are there any places in my code where I need to make minor changes?

Please log (cout) these values :
 ``123`` ``````highest = score[0,1]; secondHighest=score[0,1]; // Log... ``````

Insert log commands after these commands above.
LOG THESE VALUES THEN POST IT HERE - It's very important!
Input : 60, 50, 40.... (Your Input Above)
I doubt it's a missing checking first element problem.
Last edited on
This is my rebuilded example : (I'm VS, what your compiler are you using?)
It should correct :
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include using namespace std; const int MAX_SCORES = 10; // Maximum number of scores void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest) { highest = scores[0];///Changed secondHighest = 0;///Changed for (int i = 1; i < numScores; i++)///Changed { if(scores[i] > highest) { secondHighest = highest; highest = scores[i]; } else if(scores[i] > secondHighest) { secondHighest = scores[i]; } } } int main() { ///////////Init double scores[MAX_SCORES]; int scoreCount; double highestScore, secondHighestScore; ///////////Input cout << "Max Scores : ";cin >> scoreCount; scoreCount = (scoreCount < MAX_SCORES) ? scoreCount : MAX_SCORES; for (int i = 0; i < scoreCount; i++) {cout << "Please enter score[" << i << "] : ";cin >> scores[i];} ///////////Output getTopTwoScores(scores, scoreCount, highestScore, secondHighestScore) ; cout << "\nThe highest score : " << highestScore << " The second highest score : " << secondHighestScore << endl; system("pause"); return 0; }``````

But actually, the safest choice is :
 ``1234567891011121314151617181920212223242526`` ``````void getTopTwoScores(double scores[], int numScores, double& highest, double& secondHighest){ //////Find the lowest value double min = scores[0]; for (int i = 1; i < numScores; i++) { if (scores[i] < min) {min = scores[i];} } //////////////////////////////////////////////////////////// highest = scores[0]; secondHighest = min; for (int i = 1; i < numScores; i++) { if(scores[i] > highest) { secondHighest = highest; highest = scores[i]; } else if(scores[i] > secondHighest) { secondHighest = scores[i]; } } }``````

 ``12`` ``````highest = score[0,1]; secondHighest=score[0,1];``````

The similar code :
 ``12`` ``````highest = score[1]; secondHighest=score[1];``````

You ignored first element.

And anything else? :)

Hope all goes well....
Last edited on
Topic archived. No new replies allowed.