我需要将数组传递给函数以求平均不同的测试分数

时间:2018-10-08 04:23:22

标签: c++ arrays function

此代码的目标是通过函数传递数组(我已经很难理解)。我用笔和纸检查了一下代码,我想我只是不了解发生了什么问题。我投入的所有测试成绩都只推了一个可笑的大负数。我并不是要你们做我的作业,因为我真的想尝试理解我在做什么,但是任何帮助现在都将不胜感激。

#include <iostream>

//function prototype
double average(int studentScores[], int size);
double studentScores[4];
bool runAgain(void);

int main() {
    do {
        int studentScores[4], size = 4, result;
        string score;

        cout << "This program will calculate the average of four diffrent exam scores." << endl;

        for (int i = 0; i < size; i++) {
            studentScores[i] = 0;
            cout << "Please Enter Exam Score " << i + 1 << ": ";
            getline(cin, score);
        }
        for (int i = 0; i < size; i++) {
            result = (studentScores[1] + studentScores[2] + studentScores[3] + studentScores[4]) / size;
            studentScores[i]++;
        }

        cout << "The Average Exam score is " << result << endl;
    } while (runAgain());

    system("pause");
    return 0;
}

//function implementation
double average(int studentScores[], int size) {
    for (int i = 0; i < size; i++) {

        return (studentScores[i]++ / size);
    }
}
bool runAgain(void) {
    char userResponse;

    cout << "\nWould you like to run again (y or n): ";
    cin >> userResponse;


    if (userResponse == 'y' || userResponse == 'Y')
        return(true);

    return(false);
}

2 个答案:

答案 0 :(得分:1)

第一个明显的错误:

int studentScores[4]

studentScores有4个元素,编号为studentScores[0]studentScores[3]

但是您的代码访问

中的studentScores[4]
result = (... + studentScores[4]) / ...

不存在(并且不访问studentScores[0],但存在)。

答案 1 :(得分:0)

很高兴尝试在没有给出答案的情况下帮助您。

在整个代码中我都会出现一些问题,只要您得到意外的输出,就应该在以后的程序中问自己。

#include <iostream>

//function prototype
double average(int studentScores[], int size);
double studentScores[4];
bool runAgain(void);

int main() {
    do {
        int studentScores[4], size = 4, result;
        string score; 
        /* Above, you declared a string to store the user's input in.
           In C++, the string "4" DOES NOT equal the integer 4. 
           How will you handle the fact that the variable 'score' is of type
           string, but you want to work with integers?
           Is there an easy way to get rid of this issue? (hint: use cin)
        */

        cout << "This program will calculate the average of four diffrent exam scores." << endl;

        /* In the below for-loop, think about what the value of 'score' will be
           after each iteration. (Hint, getline replaces the existing value of score).
           Also, where is the code that saves the user's inputted numbers to an array?
           Learn how to write values to an array http://www.cplusplus.com/doc/tutorial/arrays/ 
        */

        for (int i = 0; i < size; i++) { 
            studentScores[i] = 0;
            cout << "Please Enter Exam Score " << i + 1 << ": ";
            getline(cin, score);
        }

        /* In the for-loop below, you already noticed that your array has random
           values in it. The comment about the above for-loop should address that issue.
           Equally important though is understanding what the heck is happening in this
           loop below. After you fix the bug in the for-loop above (which will
           get the values in the array to equal the user's inputs), you'll be faced 
           with issues in this loop below.
           My advice is to understand what happens when the program
           executes "studentScores[i]++". First, it gets the number in the array at 
           the ith+1 position, then it increments that number by 1. Cool, but what
           are you doing with the result of that? It's not being put to use anywhere.
           Also, note that because the array is never being updated, 
           the line above it just calculates the same number and stores it in 'result'
           every iteration of the loop.

        */
        for (int i = 0; i < size; i++) {
            result = (studentScores[1] + studentScores[2] + studentScores[3] + studentScores[4]) / size;
            studentScores[i]++;
        }

        cout << "The Average Exam score is " << result << endl;
    } while (runAgain());

    system("pause");
    return 0;
}

// this function doesn't seem to be used, but understanding what
// is wrong with it will help you understand how to code.
// First, note that once a function hits 'return', it will
// never execute any more code in that function.
// So here, your return statement in the for-loop will prevent an
// actual loop from occuring (since the function will exit as soon as the first loop iteration is entered)
// Second, note that you are just getting the value in the array and adding 1 to it
// before dividing it by 'size', which is not the formula for an average. 
double average(int studentScores[], int size) {
    for (int i = 0; i < size; i++) {

        return (studentScores[i]++ / size);
    }
}
bool runAgain(void) {
    char userResponse;

    cout << "\nWould you like to run again (y or n): ";
    cin >> userResponse;


    if (userResponse == 'y' || userResponse == 'Y')
        return(true);

    return(false);
}

我希望这些评论对您有所帮助:)继续努力! 别忘了数组从索引0开始。尝试访问studentScores [4] 会给您一个意外的电话号码。