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
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int numberStudent=4, numberQuiz=3;
void fillArray( int a[], int size, int & numberUsed);
void computeStAve(const int grade[] [numberQuiz],double stAve[]);
void computeQuizAve(const int grade[] [numberQuiz], double quizAve[]);
void display(const int grade[][numberQuiz], const double stAve[], const double quizAve[] );
int main()
{
int numberUsed;
int grade[numberStudent][numberQuiz];
double stAve[numberStudent];
double quizAve[numberQuiz];
fillArray(grade, numberQuiz, numberUsed);
computeStAve(grade, stAve);
computeQuizAve(grade, quizAve);
display(grade, stAve, quizAve);
return 0;
}
void fillArray(int a[], int size, int&numberUsed)
{
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;
}
numberUsed=index;
}
void computeStAve(const int grade[][numberQuiz], double stAve[])
{
for(int stNum=1; stNum<=numberStudent; stNum++)
{
double sum=0;
for(int quizNum=1; quizNum<=numberQuiz; quizNum++)
sum=sum+grade[stNum-1][quizNum-1];
stAve[stNum-1]=sum/numberQuiz;
}
}
void computeQuizAe(const int grade[][numberQuiz], double quizAve[])
{
for(int quizNum=1; quizNum<=numberQuiz; quizNum++)
{
double sum=0;
for(int stNum=1; stNum<=numberStudent; stNum++)
sum=sum+grade[stNum-1][quizNum-1];
quizAve[quizNum-1]=sum/numberStudent;
}
}
void display(const int grade[] [numberQuiz], const double stAve[], const double quizAve[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout<<setw(10)<<"Student"
<<setw(5)<<"Ave"
<<setw(10)<<"Quizzes\n";
for(int stNum=1; stNum<=numberStudent; stNum++)
{
cout<<setw(10)<<stNum
<<setw(5)<<stAve[stNum-1]<<" ";
for(int quizNum=1; quizNum<=numberQuiz; quizNum++)
cout<<setw(5)<<grade[stNum-1][quizNum-1];
cout<<endl;
}
cout<<"Quiz averages= ";
for(int quizNum=1; quizNum<=numberQuiz; quizNum++)
cout<<setw(5)<<quizAve[quizNum-1];
cout<<endl;
}
|