2d arrays, calling function problems
I'm doing a task, and I'm having some problems, so help please, u kind people :D I need to create function that allows input for student grades. In that function, I need to allow only input for 6-10 (passing grades). Then, I need to make a function that calculates lowest grade for student. And, in the end, need to make a function that calculates average grade per student. PS : Maybe my ideas are wrong, or u would to something different or better, say it please, I wanna learn. Thanks in advance.
He开发者_高级运维re is my code and errors :
#include <iostream>
using namespace std;
int input (int [][4], int);
int average (int [][4], int);
int min (int [][4], int);
int main ()
{
const int wdth = 5;
int matrix[4][4];
input (matrix [4][4], wdth);
average (matrix [4][4], wdth);
min (matrix [4][4], wdth);
return 0;
}
int input (int matrix[][4], int wdth)
{
for (int i = 0; i < wdth; i ++)
{
cout<<"Enter grades for "<<i+1<<" student:"<<endl;
for (int j = 0; j < wdth; j ++)
{
cin>>matrix[i][j];
if ((matrix[i][j] < 6) || (matrix[i][j] > 10))
{
cout<<"INVALID INPUT!"<<endl;
return 0;
}
//cout<<setw(5);
cout<<matrix[i][j];
}
cout<<endl;
}
return 0;
}
int average (int matrix[][4], int wdth)
{
int sum = 0;
int avrg = 0;
for (int i = 0; i < wdth; i ++)
{
cout<<"Calculating average for "<<i+1<<" student: "<<endl;
for (int j = 0; j < wdth; j ++)
{
sum = sum + matrix[i][j];
}
}
avrg = sum / 5;
return 0;
}
int min (int matrix[][4], int wdth)
{
int temp = 0;
int MIN = 10;
for (int i = 0; i < wdth; i ++)
{
cout<<"Calculating lowest grade for "<<i+1<<" student: "<<endl;
for (int j = 0; j < wdth; j ++)
{
temp = matrix[i][j];
if (temp < MIN)
{
MIN = temp;
}
}
cout<<MIN;
}
return 0;
}
ERRORS:
cpp(11) : error C2664: 'input' : cannot convert parameter 1 from 'int' to 'int [][4]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
cpp(12) : error C2664: 'average' : cannot convert parameter 1 from 'int' to 'int [][4]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
input (matrix [4][4], wdth);
Here matrix [4][4]
is an element of the matrix i.e. an int
. You need to do input(matrix,wdth);
input (matrix [4][4], wdth);
average (matrix [4][4], wdth);
min (matrix [4][4], wdth);
Try it this way. :)
input (matrix, wdth);
average (matrix, wdth);
min (matrix, wdth);
With matrix[4][4]
your passing the 4th element of the 4th row. ;)
精彩评论