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

> void main () main returns an int, not void.

> for (int i=1; i<=row; i++) Every for loop is wrong. Arrays start at 0, not 1
So for (int i=0; i<row; i++)

> void main () main returns an int, not void.

> for (int i=1; i<=row; i++) Every for loop is wrong. Arrays start at 0, not 1
So for (int i=0; i<row; i++)

Okay, thanks, i'll correct that but it still doesn't solve my problem.

Please read the concept of function and what the function of return.
return is use within any function to return a single value to a calling function in your main.Either you use array to return multiple value or call the max by using loop for every row from you main program.
I hope you understand whats the problem.........best of luck

Please read the concept of function and what the function of return.
return is use within any function to return a single value to a calling function in your main.Either you use array to return multiple value or call the max by using loop for every row from you main program.
I hope you understand whats the problem.........best of luck

Can you write an example please, i have to do it like this can't use an array to return multiple values, i got to do it in main, but i don't understand how.
Thanks

change your max function like this

maximal=0;
 
		for (int j=0; j<=col; j++)
		{
			if (mat[i][j]>maximal)
			maximal=mat[i][j];
		}
 
		return maximal;

change your max function like this

maximal=0;
 
		for (int j=0; j<=col; j++)
		{
			if (mat[i][j]>maximal)
			maximal=mat[i][j];
		}
 
		return maximal;

you mean without this part?

for (int i=1; i<=row; i++)
{

or is the setting j on zero only change, cause i tried that already but it doesn't work.

maybe if i wrote the concept of the assignment, it would help.


These 3 function can't change, code must be based on that.

void input(int[][4], int, int);
int max(int[][4],int,int,int);
float average(int[][4],int,int,int); <- it's int in the original code, i've changed it to float to avoid errors.

Got to input the grades (grades 6-10 only) in function input.
Got to find maximum grade for student 1,2,3,4,5 (rows) in function max.
Got to find average grade for subjects 1,2,3,4 (columns) in function average.

> or is the setting j on zero only change, cause i tried that already but it doesn't work.
No, the other change is changing <= into <

Post your latest code.

> or is the setting j on zero only change, cause i tried that already but it doesn't work.
No, the other change is changing <= into <

Post your latest code.

It still doesn't help i know that arrays are supposed to start from 0, but it doesn't change anything i'm still stuck at the same thing, how to get counter to reset itself and print out maximum grade for each student (row) and average grade for each subject (column) in main function.

#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=0; i<row; i++)
	{
		int maximal=0;
		cout<<"Maximal grade for student "<<i<<" is: "<<max (mat, row, col, maximal)<<endl;
	}

	for (int j=0; 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=0; i<row; i++)
	{
		for (int j=0; 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=0; i<row; i++)
	{
		maximal=0;

		for (int j=0; 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=0; j<col; j++)
	{
		averageofsub=0;
		float sum=0;
		int counter=0;
		for (int i=0; i<row; i++)
		{
			sum+=mat[i][j];
			counter++;
		}

		averageofsub=sum/float(counter);
		return averageofsub;
	}
	
}

Messed up arguments in both of the functions, got a little help from a friend,
this was what i was looking for, now it works, thanks for you help guys.

The solution:

#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,int);
     
     
     
    void main ()
    {
     
    const int row=5;
    const int col=4;
     
    int mat[row][col];
     
    input (mat, row, col);
     
    for (int i=0; i<row; i++)
    {
    cout<<"Maximal grade for student "<<i<<" is: "<<max (mat, row, col, i)<<endl;
    }
     
    for (int j=0; j<col; j++)
    {
    cout<<"Average grade of subject "<<j<<" is: "<<average(mat, row, col, j)<<endl;
    }
     
     
     
     
     
    system ("PAUSE");
    }
     
     
    void input (int mat[][4], int row, int col)
    {
    for (int i=0; i<row; i++)
    {
    for (int j=0; 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 student)
    {
     
	int maximal=0;
    for (int j=0; j<col; j++)
    {
    if (mat[student][j]>maximal)
    maximal=mat[student][j];
    }
     
    return maximal;
     
     
    }
     
    float average (int mat[][4], int row, int col, int subject)
    {
     
	int sum=0;

    for (int i=0; i<row; i++)
	{
	sum+=mat[i][subject];
	}
     
    return (float)sum/(float)row;

    }

Edited 5 Years Ago by klikeras: n/a

This question has already been answered. Start a new discussion instead.