C ++ 2D数组函数

时间:2012-11-08 18:56:13

标签: c++ arrays double

基本上我有,

    double marks [STUDENTS][ASSIGNMENTS] = {    (0.1,0.2,0.3,0.4,0.5,0.6,0.7),
                                            (1,0.9,0.8,0.7,0.6,0.5,0.5),
                                            (0.8,0.8,0.8,0.8,0.8,0.8,0.8),
                                            (0.8,0.9,0.7,0.8,0.9,0.7,0.8),
                                            (0.5,0.6,0.7,0.8,0.9,0.5,0.9)};

我想从double studentAverages [STUDENTS]

获得studentAverages=calculateStudentAverages(marks);

使用:

    double calculateStudentAverages (double marks[STUDENTS][ASSIGNMENTS]){
    double averages[STUDENTS];
    double average;
    for (int i = 0; i < STUDENTS; i++) {
            for (int j = 0; j < ASSIGNMENTS; j++) {
                average = average + marks[i][j];

            }
            averages[i]=average/ASSIGNMENTS;

    }    
    return averages;
}

但是我得到“无法从'double [5]'转换为'double'”和“无法从'double'转换为'double'[5]'”

1 个答案:

答案 0 :(得分:3)

您的一个错误来自于您的函数被声明为返回单个double,但您正在尝试返回doubles的数组。因此,您需要更改功能标题以适应该标题,或者只返回一个double

与错误无关,但您在启动时绝对应将average初始化为0.0,否则您在计算中会有未定义的行为