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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
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?