Hello,
I need help with an assignment, been trying to figure it out for couple of hours with no luck, hope someone here can help me, i would be extremely grateful.
I need to create a 2D array 5x4, each row represent a student and each column respresents a subject.
Got 3 functions to do,
void input(int[][4], int, int);
int max(int[][4],int,int,int);
float average(int[][4],int,int,float);
I can handle the input but the other 2 are bugging me.
In the max function i have to get max grade of each student (each row) and print it out in main function.
In the average function i got to get average grade of each subject (each col) and print it out on main.
In my code i get maximum grade for only the first row, and average grade of the first subject (first column).
I don't know how to reset the counter so it prints out maximum grade of each student and average grade of each subject.
Would appreciate some help.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
void input(int[][4], int, int);
int max(int[][4],int,int,int);
float average(int[][4],int,int,float);
void main ()
{
const int row=5;
const int col=4;
int mat[row][col];
input (mat, row, col);
for (int i=1; i<=row; i++)
{
int maximal=0;
cout<<"Maximal grade for student "<<i<<" is: "<<max (mat, row, col, maximal)<<endl;
}
for (int j=1; j<=col; j++)
{
float averageofsub=0;
cout<<"Average grade of subject "<<j<<" is: "<<average(mat, row, col, averageofsub)<<endl;
}
system ("PAUSE");
}
void input (int mat[][4], int row, int col)
{
for (int i=1; i<=row; i++)
{
for (int j=1; j<=col; j++)
{
do{
cout<<"Input ["<<i<<"]["<<j<<"] element: ";
cin>>mat[i][j];
if (mat[i][j]<6||mat[i][j]>10)
cout<<"You've entered a wrong grade!"<<endl;
}
while (mat[i][j]<6||mat[i][j]>10);
}
}
}
int max (int mat[][4], int row, int col, int maximal)
{
for (int i=1; i<=row; i++)
{
maximal=0;
for (int j=1; j<=col; j++)
{
if (mat[i][j]>maximal)
maximal=mat[i][j];
}
return maximal;
}
}
float average (int mat[][4], int row, int col, float averageofsub)
{
for (int j=1; j<=col; j++)
{
averageofsub=0;
float sum=0;
int counter=0;
for (int i=1; i<=row; i++)
{
sum+=mat[i][j];
counter++;
}
averageofsub=sum/float(counter);
return averageofsub;
}
}