#include <iostream> #include <cmath> #include <string> #include <math.h> using namespace std; //Functions int studentScore(); void average(); //Variables int scoreInput[3]; int countScore; double sAverage; int main () { studentScore(); system("pause"); return 0; } //Part 5 void average() { sAverage = studentScore() / 4; cout << sAverage << endl; return average(); } //Part 6 int studentScore() { //Looping countScore and countInput 15x for (countScore = 1; countScore <= 3; countScore++) { cout << "Please Enter a Score: "; cin >>scoreInput[countScore]; } for (countScore = 1; countScore <= 3; countScore++) { cin >>scoreInput[countScore]; break; } return 0; }

# Help on finding average of a for loop

Page 1 of 1## 3 Replies - 648 Views - Last Post: 05 October 2012 - 04:45 PM

### #1

# Help on finding average of a for loop

Posted 05 October 2012 - 03:40 PM

I need some help with finding the average of this for loop. The assignment requires that it be in individual functions.

##
**Replies To:** Help on finding average of a for loop

### #2

## Re: Help on finding average of a for loop

Posted 05 October 2012 - 03:45 PM

What problems are you encountering?

As an aside, I can already see that you have a buffer overrun. Remember that C/C++ arrays are zero based, but you are accessing your arrays with indices 1 to 3.

As an aside, I can already see that you have a buffer overrun. Remember that C/C++ arrays are zero based, but you are accessing your arrays with indices 1 to 3.

### #3

## Re: Help on finding average of a for loop

Posted 05 October 2012 - 03:49 PM

The problem I'm having isn't an error, but it's not averaging the numbers that the user inputs.

### #4

## Re: Help on finding average of a for loop

Posted 05 October 2012 - 04:45 PM

Well, ignoring for now that you have a buffer overrun in studentScore(), your function studentScore() is return 0, rather than the sum of the scores. So when you try to compute the average in average(), 0 divided by 4, results in an average of zero.

Assuming that you fix things such that you are computing the sum of the scores, you are only inputting 3 scores (twice) in studentScore(). Then later when you try to compute the average, you are dividing by 4.

Assuming that you fix things such that you are computing the sum of the scores, you are only inputting 3 scores (twice) in studentScore(). Then later when you try to compute the average, you are dividing by 4.

Page 1 of 1