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

C++ 我需要将数组传递给函数以平均不同的测试分数,c++,arrays,function,C++,Arrays,Function,这段代码的目标是通过一个函数传递一个数组,我已经很难理解这个函数了。我用笔和纸写了一遍代码,我想我只是不知道怎么回事。我所有的考试分数都是一个大得离谱的负数。我不是要你们帮我做作业,因为我真的很想试着理解我在做什么,但我现在真的很感激你们的帮助 #include <iostream> //function prototype double average(int studentScores[], int size); double studentScores[4]; bool ru

这段代码的目标是通过一个函数传递一个数组,我已经很难理解这个函数了。我用笔和纸写了一遍代码,我想我只是不知道怎么回事。我所有的考试分数都是一个大得离谱的负数。我不是要你们帮我做作业,因为我真的很想试着理解我在做什么,但我现在真的很感激你们的帮助

#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);
}
第一个明显的错误:

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

但您的代码可以访问中的studentScores[4]


它不存在,也无法访问studentScores[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开始。正在尝试访问学生分数[4]
将给您一个意外的数字。

我建议您阅读有关调试代码的提示。您可以添加cout语句来打印变量的值,并查看这些值是否与您通过纸笔跟踪确定的值相匹配。当你发现一个地方的输出与你期望的不同时,你应该用你的问题来解释它发生的地方。然后,我们可以通过解释导致问题和绊倒您的任何概念来帮助您。欢迎访问Jayus网站!我会附和代码学徒说的话;他们链接的页面包含一系列提示,这些提示可能会帮助您自己找到问题所在。即使您不这样做,这将是将来调试其他代码的良好实践,并且它可能还将帮助您将发布在此处的示例程序缩减为显示相同错误的较小程序,这反过来将使您的问题更好,更容易得到回答。除了我之前的评论之外,你应该花点时间思考一下如何计算四个数字的平均值。你能用文字描述一下你手工做这件事的步骤吗?你的标题不好,因为它没有描述问题。没有涉及任何函数,您的代码从不调用平均值。@melpomene是的,这绝对是个问题。我能够在没有函数的情况下制作一个平均分数的程序,但是作业需要一个,所以我试着在其中加入一个,但我不知道如何使用数组。好的,这是有意义的。所以不是从1开始计数,而是从0开始?另外,当我计算结果时,是在第二个for循环内还是在第二个for循环外?@Jayus,这是您的代码。你应该能够解释它的每一部分都有什么作用。就这点而言,为什么会有第二个For循环呢?老实说,我认为它存在的唯一原因是因为我教授的示例代码有第二个For循环。我不完全理解数组和循环,因为我们上周才讨论过。我一直认为代码需要多个循环才能编译。
#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);
}