错误:尝试传递数组时,从“int *”到“int”的转换无效

时间:2018-04-21 19:40:41

标签: c++ arrays pointers int

我在C ++类中,对于一个项目,我们需要使用数组计算成绩,并且至少需要3个函数来计算。 到目前为止我的代码:

#include <iostream>

using namespace std;

double gradeAverage(double, double, double, double);
double letterGrade(double);
double arrayAverage(int, int);
void droppedQuizzes(int, int);


int main(){
    int quizGrades[7] = {100, 0, 50, 30, 40, 100, 0};
    int testGrades[2] = {50, 54};
    int projectGrades[4] = {100, 0, 90, 95};
    int labGrades[6] = {100, 100, 0, 50, 60, 100};
    int newQuizGrades[5] = {0, 0, 0, 0, 0};



    droppedQuizzes(quizGrades, newQuizGrades);


    double quizAve = arrayAverage(quizGrades, 7);
    double testAve = arrayAverage(testGrades, 2);
    double projAve = arrayAverage(projectGrades, 4);
    double labAve = arrayAverage(labGrades, 6);

    double gradeAve = gradeAverage(quizAve, testAve, projAve, labAve);

    char finalGrade = letterGrade (gradeAve);

    cout << "Final Numeric Average: " << gradeAve << endl << "Letter Grade: " << finalGrade << endl;

    return 0;
}

double gradeAverage(double quiz, double test, double proj, double lab){
    double quizWeighted = quiz * 0.2;
    double testWeighted = test * 0.25;
    double projWeighted = proj * 0.2;
    double labWeighted = lab * 0.15;
    double finalWeighted = 0.2; // assuming 100 as the score.

    return (quizWeighted + testWeighted + projWeighted + labWeighted + finalWeighted);
}

double LetterGrade(double ave){
    if(ave >= 90)
        return 'A';
    else if(ave >= 80)
        return 'B';
    else if(ave >= 70)
        return 'C';
    else if(ave >= 65)
        return 'D';
    else
        return 'F';
}

double arrayAverage(int arr[], int size){
    double sum = 0;
    for(int x = 0; x < size; x++){
        sum += arr[x];
    }
    return (sum / size);
}

void droppedQuizzes(int quizzes[7], int newQuizzes[5]){
    int low1 = 100, low2 = 100, count = 0;


    for(int x = 0; x < 7; x++){
        if(quizzes[x] < low2)
            if(quizzes[x] < low1)
                low1 = quizzes[x];
            else
                low2 = quizzes[x];
        else{
            newQuizzes[count] = quizzes[x];
            count++;
        }
    }
}

我收到很多错误,但是对于不同的变量,他们都说同样的事情:

Lab6.cpp:20:42: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
  droppedQuizzes(quizGrades, newQuizGrades);
                                          ^
Lab6.cpp:8:6: error:   initializing argument 1 of ‘void droppedQuizzes(int, int)’ [
 void droppedQuizzes(int, int);
      ^

我知道*表示指针,但我不明白为什么我无法通过数组,我之前已经在其他代码中完成了。

一个解决方案将不胜感激,更多的解释。谢谢!

1 个答案:

答案 0 :(得分:0)

函数原型必须指定与函数定义相同的签名。所以这些界限:

double arrayAverage(int, int);
void droppedQuizzes(int, int);

应该是:

double arrayAverage(int[], int);
void droppedQuizzes(int[], int[]);