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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
//DISPLAY 7.14 Two-Dimensional Array
//Reads quiz scores for each student into the two-dimensional array grade (but
//the input code is not shown in this display). Computes the average score
//for each student and the average score for each quiz. Displays the quiz scores
//and the averages.
#include <iostream>
#include <iomanip>
const int NUMBER_STUDENTS = 4, NUMBER_QUIZZES = 3;
void fill_array(int a[], int size, int& number_used);
void compute_st_ave(const int grade[][NUMBER_QUIZZES], double st_ave[]);
//Precondition: Global constants NUMBER_STUDENTS and NUMBER_QUIZZES
//are the dimensions of the array grade. Each of the indexed variables
//grade[st_num-1, quiz_num-1] contains the score for student st_num on quiz
//quiz_num.
//Postcondition: Each st_ave[st_num-1] contains the average for student
//number stu_num.
void compute_quiz_ave(const int grade[][NUMBER_QUIZZES], double quiz_ave[]);
//Precondition: Global constants NUMBER_STUDENTS and NUMBER_QUIZZES
//are the dimensions of the array grade. Each of the indexed variables
//grade[st_num-1, quiz_num-1] contains the score for student st_num on quiz
//quiz_num.
//Postcondition: Each quiz_ave[quiz_num-1] contains the average for quiz number
//quiz_num.
void display(const int grade[][NUMBER_QUIZZES],
const double st_ave[], const double quiz_ave[]);
//Precondition: Global constants NUMBER_STUDENTS and NUMBER_QUIZZES are the
//dimensions of the array grade. Each of the indexed variables grade[st_num-1,
//quiz_num-1] contains the score for student st_num on quiz quiz_num. Each
//st_ave[st_num-1] contains the average for student stu_num. Each
//quiz_ave[quiz_num -1] contains the average for quiz number quiz_num.
//Postcondition: All the data in grade, st_ave, and quiz_ave has been output.
int main( )
{
using namespace std;
int grade[NUMBER_STUDENTS][NUMBER_QUIZZES];
double st_ave[NUMBER_STUDENTS];
double quiz_ave[NUMBER_QUIZZES];
//The code for filling the array grade goes here, but is not shown.
fill_array (grade, NUMBER_STUDENTS,NUMBER_QUIZZES);
compute_st_ave(grade, st_ave);
compute_quiz_ave(grade, quiz_ave);
display(grade, st_ave, quiz_ave);
return 0;
}
void fill_array(int a[], int size, int& number_used)
{
using namespace std;
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of the list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
number_used = index;
}
void compute_st_ave(const int grade[][NUMBER_QUIZZES], double st_ave[])
{
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
{//Process one st_num:
double sum = 0;
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
sum = sum + grade[st_num -1][quiz_num -1];
//sum contains the sum of the quiz scores for student number st_num.
st_ave[st_num -1] = sum/NUMBER_QUIZZES;
//Average for student st_num is the value of st_ave[st_num-1]
}
}
void compute_quiz_ave(const int grade[][NUMBER_QUIZZES], double quiz_ave[])
{
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
{//Process one quiz (for all students):
double sum = 0;
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
sum = sum + grade[st_num -1][quiz_num -1];
//sum contains the sum of all student scores on quiz number quiz_num.
quiz_ave[quiz_num -1] = sum/NUMBER_STUDENTS;
//Average for quiz quiz_num is the value of quiz_ave[quiz_num-1]
}
}
//Uses iostream and iomanip:
void display(const int grade[][NUMBER_QUIZZES],
const double st_ave[], const double quiz_ave[])
{
using namespace std;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << setw(10) << "Student"
<< setw(5) << "Ave"
<< setw(15) << "Quizzes\n";
for (int st_num = 1; st_num <= NUMBER_STUDENTS; st_num++)
{//Display for one st_num:
cout << setw(10) << st_num
<< setw(5) << st_ave[st_num-1] << " ";
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
cout << setw(5) << grade[st_num -1][quiz_num -1];
cout << endl;
}
cout << "Quiz averages = ";
for (int quiz_num = 1; quiz_num <= NUMBER_QUIZZES; quiz_num++)
cout << setw(5) << quiz_ave[quiz_num -1];
cout << endl;
}
|