I have this code from my professor in a C++ class. #include <iostream> #include
ID: 3731743 • Letter: I
Question
I have this code from my professor in a C++ class.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int sizeRow = 5;
const int sizeCol = 5;
**** fill up with a two-dimensional array declaration using sizeRow and sizeCol ****
int row, col;
int sum;
for (row = 0; row < sizeRow; row++)
{
for (col = 0; col < sizeCol-1; col++)
**** fill up with an input statement to store into the array ****
}
for (row = 0; row < sizeRow; row++)
{
sum = 0;
for (col = 1; col < sizeCol - 1; col++)
sum = sum + scores[row][col];
**** fill up with a statement to calculate the average and to store into the array ****
}
cout <<setw(12) << "Student ID" << setw(10) << "exam 1" << setw(10) << "exam 2" << setw(10) << "exam 3" << setw(10) << "average" << endl;
for (row=0; row <sizeRow; row++)
{
**** fill up with statements to print the student ID, 3 exam scores and the average from the array ****
}
return 0;
}
The input data is :
Example Input Data
Student ID Exam1 Exam 2 Exam 3
4169 88 72 82
8126 67 79 71
2049 72 87 86
5837 96 100 89
3576 62 71 68
I understand the two-dimensional array declaration, but after that I can't follow what he is doing with the code.
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int sizeRow = 5;
const int sizeCol = 5;
// **** fill up with a two-dimensional array declaration using sizeRow and sizeCol ****
int arr[sizeRow][sizeCol];
int row, col;
int sum;
for (row = 0; row < sizeRow; row++)
{
for (col = 0; col < sizeCol-1; col++)
// **** fill up with an input statement to store into the array ****
cin>>arr[row][0]>>arr[row][1]>>arr[row][2]>>arr[row][3];
}
for (row = 0; row < sizeRow; row++)
{
sum = 0;
for (col = 1; col < sizeCol - 1; col++)
sum = sum + scores[row][col];
//**** fill up with a statement to calculate the average and to store into the array ****
arr[row][4] = sum;
}
cout <<setw(12) << "Student ID" << setw(10) << "exam 1" << setw(10) << "exam 2" << setw(10) << "exam 3" << setw(10) << "average" << endl;
for (row=0; row <sizeRow; row++)
{
// **** fill up with statements to print the student ID, 3 exam scores and the average from the array ****
cout <<setw(12) << arr[row][0] << setw(10) << arr[row][1] << setw(10) << arr[row][2] << setw(10) << arr[row][3] << setw(10) << arr[row][4] << endl;
}
return 0;
}