c++ - 二维数组,调用函数问题

标签 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

最佳答案

input (matrix [4][4], wdth); 这里 matrix [4][4] 是矩阵的一个元素,即 int 。你需要做 input(matrix,wdth);

关于c++ - 二维数组,调用函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5140700/

相关文章:

c++ - 在 C++ 中过滤来自 istream 的非法输入

c++ - 可以在运行时优化浮点零乘法吗?

c++ - 在 C++ 中,我怎样才能从一个对象到一个静态成员?

c++ - 如何在 C++ 中创建任意函数的字符串参数化包装器?

c++ - 创建网格数组

c++ - Failed with error 87 : The parameter is incorrect. 如何确定哪个参数不正确?

c++ - const TypedeffedIntPointer 不等于 const int *

c++ - 按照每对权重之和的顺序,高效地计算两个加权集合中的对

c++:检查数组的顺序

c++ - 如何确定任意进程是否正在 wow64 下运行?