3 students, each with 4 grades (assignments).
Input their data and determine each
student’s avg as well as the class avg in each assignment.
/*2-Dimensional Arrays*/
# include <iostream>
using namespace std;
const int rows=4;
const int cols=5;
void initialize (double grades[][cols])
{
for(int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
grades[i][j]=0;
}
void read_array(double grades[][cols])
{
for (int i=0;i<=rows-2;i++)
{
cout<<"Enter grades for student "<<i+1<<"\n";
for(int j=0;j<=cols-2;j++)
cin>>grades[i][j];
}
cout<<"\n";
}
void students_avg(double grades[][cols])
{
for(int i=0;i<=rows-2;i++)
{
for (int j=0;j<=cols-2;j++)
grades[i][cols-1]+=grades[i][j];
grades[i][cols-1]/=(cols-1);
}
}
void class_avg(double grades[][cols])
{
for(int j=0;j<cols-1;j++)
{
for(int i=0;i<rows-1;i++)
grades [rows-1][j]+=grades[i][j];
grades[rows-1][j]/=(rows-1);
}
}
void print_array(double grades[][cols])
{
cout<<"\t\tGrades\t\t\t\tAverage\n"<<endl;
for (int i=0;i<=rows-2;i++)
{
cout<<"Student"<<i+1<<"\t";
for(int j=0;j<cols;j++)
cout<<grades[i][j]<<"\t";
cout<<endl;
}
cout<<"\nAverage\t\t";
for (int j=0;j<=cols-2;j++)
cout<<grades[rows-1][j]<<"\t";
cout<<endl;
}
int main()
{
double grades[rows][cols];
initialize(grades);
read_array(grades);
students_avg(grades);
class_avg(grades);
print_array(grades);
cout<<endl;
system("Pause");
return 0;
}