1. Open the TestScores.cpp file from the Chapter05 folder of the data files prov
ID: 3554438 • Letter: 1
Question
1. Open the TestScores.cpp file from the Chapter05 folder of the data files provided with
your book. Alternatively, you can type the application yourself, as shown in Figure 5-47.
The program declares a constant that is used to create an array that can hold 10 student
test scores. In a for loop, the user enters 10 scores. At the end of the data entry loop, the scores are totaled and displayed, and then their average is calculated and displayed.
FIGURE 5-47
#include<iostream>
using namespace std;
int main()
{
const int NUM_SCORES = 10;
int score[NUM_SCORES];
int sub;
double total = 0;
double average = 0;
for(sub = 0; sub < NUM_SCORES; ++sub)
{
}
cout << "Enter score #" << (sub + 1) << " ";
cin >> score[sub];
cout << endl << "The scores are:" << endl;
for(sub = 0; sub < NUM_SCORES; ++sub)
{
total += score[sub];
cout << score[sub] << " ";
}
cout << endl;
average = total / NUM_SCORES;
cout << "The average score is " <<
average << endl;
return 0;
2. Run the program. Respond to the prompts with test scores such as 70, 80, 90, and so on.
After you enter all 10 scores, confirm that the output displays them all and that the average is correct.
3. The existing program accepts exactly 10 scores. You can make the program more flexible
by allowing fewer scores if the user enters a sentinel value. To allow the user to enter scores
until typing 999, make the following changes:
a. Initialize
sub to 0:
int sub = 0;
b. After the existing constant and variable declarations, add a new constant to hold the
sentinel value:
const int END = 999;
c. On the next line, get the first score and store it in element 0 of the array,
cout << "Enter score #" << (sub + 1) << " or " <<
END << " to quit ";
cin >> score[sub];
d. Add 1 to sub so the next data entry will be stored in score[1], then remove the existing for loop and replace it with a while loop. The loop continues while two conditions remain true: the subscript remains within range and the previous score entered is not the END:
while(sub < NUM_SCORES && score[sub - 1] != END)
{
}
cout << "Enter score #" << (sub + 1) << " or "<<
END << " to quit ";
cin >> score[sub];
++sub;
It is necessary to check score[sub - 1]for the sentinel value because after the user
enters 999, the last part of the
for loop will increase sub, so the 999 will be located in
the previous location.
e. The number of scores entered might be fewer than 10, so, with the other variables,
declare a variable that can hold the number of items entered:
int scoresEntered;
f. Immediately after the closing curly brace of the data entry loop, assign the number of
entries,
sub - 1, to the scoresEntered variable:
scoresEntered = sub - 1;
g. The loop that totals and displays the entered scores should no longer loop until the tenth
item. If you do not change the loop, garbage items after the 999 will display. Change the
middle portion of the
for loop to only go as far as the last entered score before the
sentinel:
for(sub = 0; sub < scoresEntered; ++sub)
h. The average score should no longer be calculated by dividing by 10. Change the average
calculation to use scores Entered, unless 0 scores were entered (in Other words, unless the user entered 999 To quit immediately), in which case the average should be zero.
if(scoresEntered == 0)
average = 0;
else
average = total / scoresEntered;
Explanation / Answer
1.
#include<iostream>
using namespace std;
int main()
{
const int NUM_SCORES = 10;
int score[NUM_SCORES];
int sub;
double total = 0;
double average = 0;
for(sub = 0; sub < NUM_SCORES; ++sub)
{
cout << "Enter score #" << (sub + 1) << " ";
cin >> score[sub];
}
cout << endl << "The scores are:" << endl;
for(sub = 0; sub < NUM_SCORES; ++sub)
{
total += score[sub];
cout << score[sub] << " ";
}
cout << endl;
average = total / NUM_SCORES;
cout << "The average score is " << average << endl;
return 0;
}
2.
Enter score #1 10
Enter score #2 20
Enter score #3 30
Enter score #4 40
Enter score #5 50
Enter score #6 60
Enter score #7 70
Enter score #8 80
Enter score #9 90
Enter score #10 100
The scores are:
10 20 30 40 50 60 70 80 90 100
The average score is 55
3.
#include<iostream>
using namespace std;
int main()
{
const int NUM_SCORES = 10;
int score[NUM_SCORES];
int sub = 0;
double total = 0;
double average = 0;
const int END = 999;
int scoresEntered;
cout << "Enter score #" << (sub + 1) << " or " << END << " to quit ";
cin >> score[sub];
++sub;
while(sub < NUM_SCORES && score[sub - 1] != END)
{
cout << "Enter score #" << (sub + 1) << " or "<< END << " to quit ";
cin >> score[sub];
++sub;
}
scoresEntered = sub - 1;
cout << endl << "The scores are:" << endl;
for(sub = 0; sub < scoresEntered; ++sub)
{
total += score[sub];
cout << score[sub] << " ";
}
cout << endl;
if(scoresEntered == 0)
average = 0;
else
average = total / scoresEntered;
cout << "The average score is " << average << endl;
return 0;
}
Enter score #1 or 999 to quit 10
Enter score #2 or 999 to quit 20
Enter score #3 or 999 to quit 999
The scores are:
10 20
The average score is 15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.