Sie sind auf Seite 1von 2

#include <iostream>

#include <iomanip>

const int NUMBER_STUDENTS = 2, NUMBER_QUIZZES = 2;

void compute_st_ave(const int grade[][NUMBER_QUIZZES], double st_ave[]);


void compute_quiz_ave(const int grade[][NUMBER_QUIZZES], double quiz_ave[]);
void display(const int grade[][NUMBER_QUIZZES], const double st_ave[], const double quiz_ave[]);

int main()
{
using namespace std;
int grade[NUMBER_STUDENTS][NUMBER_QUIZZES];
double st_ave[NUMBER_STUDENTS];
double quiz_ave[NUMBER_QUIZZES];

for(int i = 1; i <= NUMBER_STUDENTS; i++)


{

cout << "Enter student number "<< i << " quizzes: " <<endl;
for(int j = 1; j <= NUMBER_QUIZZES; j++)
{
cin>>grade[i-1][j-1];
}

}
compute_st_ave(grade, st_ave);
compute_quiz_ave(grade, quiz_ave);
display(grade, st_ave, quiz_ave);

return 0;
}
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;
}