Define a getScores() function with the specification and prototype shown below:
// Get scores from console and return the number of scores recorded.
// A negative value signifies end of input.
// If more than MAX_SCORES values are entered then discard entries beyond the first MAX_SCORES.
// Assume that MAX_SCORES is already defined as a global constant
int getScores(double scores[] ) ;
Here is the driver (main()) I 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 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_SCORES = 10; // Maximum number of scores
int getScores(double scores[] ) ;
void printScores(double scores[], int numScores);
int main() {
double nums[MAX_SCORES];
int count;
cout << fixed << setprecision(2);
count = getScores(nums);
if (count > 0)
{
cout << count << " score"
<< (count == 1 ? " was" : "s were")
<< " recorded: ";
printScores(nums, count);
}
else
cout << "No scores were recorded. " << endl;
return 0;
}
void printScores(double scores[], int numScores)
{
for (int i = 0; i <numScores; i++)
cout << scores[i] << " ";
cout << endl;
}
|
Here is my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
int getScores(double scores[] )
{
int value;
int count = 0;
cout << "Please enter up to 10 scores, followed by -1.\n";
while(value != -1)
{
cin >> value;
if(value != -1 && count <= MAX_SCORES)
{
scores[count] = value;
count++;
}
else if(count >= MAX_SCORES)
{
cout << "Too many scores. The last " << count
<< (count == 1 ? " was" : " were")
<< " discarded\n ";
return MAX_SCORES;
}
else if(value == -1)
{
return count;
}
}
}
|
Here is the output I am only getting:
Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 -1
Too many scores. The last 10 were discarded
10 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
Here are the expected outputs I need to get:
Please enter up to 10 scores, followed by -1.
54.5 33.5 98.0 79.0 -1
4 scores were recorded: 54.50 33.50 98.00 79.00
Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 -1
10 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
Please enter up to 10 scores, followed by -1.
-1
No scores entered.
Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 6 7 8 9 10 -1
Too many scores, the last 1 was discarded
10 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
Please enter up to 10 scores, followed by -1.
0 1 2 3 4 5 -1
5 scores were recorded: 0.00 1.00 2.00 3.00 4.00 5.00
What I can do to fix this problem? Do I need to change some lines of code?