2d数组,调用函数问题

时间:2011-02-28 10:20:32

标签: c++

我正在做一项任务,我遇到了一些问题,所以请帮忙,亲切的人:D 我需要创建允许输入学生成绩的功能。在该功能中,我只需要输入6-10(通过成绩)。然后,我需要创建一个计算学生最低成绩的函数。最后,需要制作一个计算每个学生平均成绩的函数。 PS:也许我的想法是错的,或者你会想要不同或更好的东西,请说,我想学习。提前谢谢。

这是我的代码和错误:

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

错误:

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

2 个答案:

答案 0 :(得分:5)

input (matrix [4][4], wdth);此处matrix [4][4]是矩阵的元素,即int。您需要执行input(matrix,wdth);

答案 1 :(得分:3)

input (matrix [4][4], wdth);
average (matrix [4][4], wdth);
min (matrix [4][4], wdth);

以这种方式试试。 :)

input (matrix, wdth);
average (matrix, wdth);
min (matrix, wdth);

使用matrix[4][4]传递第4行的第4个元素。 ;)