-3

The goal of this code is to pass an array through a function (which I'm already having a difficult time understanding). I went through with a pen and paper and traced the code and I think I just don't know enough to understand what's going wrong. All the test scores I throw in just push back a ridiculously large negative number. I'm not asking for you guys to do my homework because I really want to try and understand what I'm doing, but any help would really be appreciated right now.

#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);
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
Jayus
  • 1
  • 3
    I suggest that you read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips on debugging your code. You can add `cout` statements to print the values of variables and see if those match what you determined by tracing through with pen and paper. When you find a spot where the output differs from what you expect, you should [edit] your question to explain where that occurs. Then we can help by explaining whatever concept that causes the problem and is tripping you up. – Code-Apprentice Oct 08 '18 at 04:25
  • 2
    Welcome to the site Jayus! I'd echo what Code-Apprentice said; the page they linked contains a bunch of tips that might help you track down the problem yourself. Even if you don't, it'll be good practice for debugging other code in the future, and it will probably also help you trim down the sample program you posted here to a smaller program which exhibits the same error - and that, in turn, will make your question better and more likely to get answered. – David Z Oct 08 '18 at 04:27
  • 2
    In addition to my previous comment, you should take a moment to think about how you calculate the average of four numbers. Can you describe in words the steps that you take to do this by hand? – Code-Apprentice Oct 08 '18 at 04:32
  • Your title is bad because it doesn't describe the problem. There is no function involved (your code never calls `average()`). – melpomene Oct 08 '18 at 04:34
  • @melpomene yeah that's definitely a problem. I was able to make a program that averaged out the scores without a function, but the assignment calls for one so I tried to throw one in there, but I don't know how to use with arrays. – Jayus Oct 08 '18 at 04:57

2 Answers2

1

First obvious bug:

int studentScores[4]

studentScores has 4 elements, numbered studentScores[0] through studentScores[3].

But your code accesses studentScores[4] in

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

which doesn't exist (and doesn't access studentScores[0], which does).

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • okay that make sense. so instead of counting from 1 i should start from 0 instead? also when i calculate the result would that be inside or outside the second for loop? – Jayus Oct 08 '18 at 04:36
  • @Jayus It's your code. You should be able to explain what every part of it does. For that matter, why is there a second for loop at all? – melpomene Oct 08 '18 at 04:40
  • Honestly, I think the only reason its there is because my professor's "example code" had a second for loop. I don't fully understand arrays and loops as we only covered it last week. I was under the belief that you needed multiple loops for the code to even compile. – Jayus Oct 08 '18 at 04:54
0

Glad to try to help ya out without giving you the answer.

I'm gonna sprinkle some questions throughout your code that you should be asking yourself in future programs whenever you get unexpected output.

#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);
}

I hope these comments helped :) Keep at it! Don't forget that arrays start at index 0. Trying to access studentScores[4] will give you an unexpected number.