Since the value in the second pair of brackets is going to be specified in more
ID: 644124 • Letter: S
Question
Since the value in the second pair of brackets is going to be specified in more than one place in the program, readability and maintenance will be improved by using a named constant for this purpose. Add the following to the start of the program; note that the number of scores for each student is being increased from three to four.
Now add the following prototype for function readScores:
Modify the last code fragment added to function main as follows; note that the number of students is being increased to allow for a larger example.
If it is decided that a different number of exams is appropriate, only the line defining constant EXAM_COUNT needs to be changed.
The function header of function readScores's definition will be identical to the function's prototype given earlier. The function body will consist of a sentinel-controlled loop to read scores until a negative one is entered. Since there are actually four scores being read, the temporary variable will be used only to check the first value (looking for the sentinel).
This loop is actually examining the first value the user enters before ever attempting to read the other three. If that first value is the sentinel (-1), the other three values are not required, and the loop stops without incrementing the student count.
Add an additional function writeScores which will display the scores of each student; the prototype follows.
Adjust function main to test this function as well.
Code Illustrationconst int EXAM_COUNT = 4;Since the value in the second pair of brackets is going to be specified in more than one place in the program, readability and maintenance will be improved by using a named constant for this purpose. Add the following to the start of the program; note that the number of scores for each student is being increased from three to four. Adjust function main to test this function as well. This loop is actually examining the first value the user enters before ever attempting to read the other three. If that first value is the sentinel ( - 1), the other three values are not required, and the loop stops without incrementing the student count. Add an additional function writeScores which will display the scores of each student; the prototype follows. If it is decided that a different number of exams is appropriate, only the line defining constant EXAM_COUNT needs to be changed.The function header of function readScores's definition will be identical to the function's prototype given earlier. The function body will consist of a sentinel - controlled loop to read scores until a negative one is entered. Since there are actually four scores being read, the temporary variable will be used only to check the first value (looking for the sentinel). Modify the last code fragment added to function main as follows; note that the number of students is being increased to allow for a larger example.Now add the following prototype for function readScores:
Explanation / Answer
#include <iostream>
using namespace std;
const int EXAM_COUNT = 4;
int readScores (int studentScores [][EXAM_COUNT], int max_student_count){
int val, count = 0;
for(int i = 0; i < max_student_count; ++i){
cin >> val;
if(val < 0){
break;
}
else{
studentScores[i][0] = val;
for(int j = 1; j < EXAM_COUNT; ++j){
cin >> val;
studentScores[i][j] = val;
}
}
count++;
}
return count;
}
void writeScores (int studentScores [][EXAM_COUNT], int numberOfStudents){
for(int i = 0; i < numberOfStudents; ++i){
cout << "Student " << (i + 1) << endl;
for(int j = 0; j < EXAM_COUNT; ++j){
cout << studentScores[i][j] << " ";
}
cout << endl;
cout << endl;
}
cout << endl;
}
int main(){
const int MAX_SIZE = 5;
int studentScores[MAX_SIZE][EXAM_COUNT];
int studentCount = readScores (studentScores, MAX_SIZE);
writeScores(studentScores, studentCount);
return 0;
}