# Help on finding average of a for loop

Page 1 of 1

## 3 Replies - 782 Views - Last Post: 05 October 2012 - 04:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294451&amp;s=e55fff8892fd3cbd304dfef969bcfb45&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alexb9054

Reputation: 0
• Posts: 4
• Joined: 05-October 12

# 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.

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

Is This A Good Question/Topic? 0

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

### #2 Skydiver

• Code herder

Reputation: 6063
• Posts: 20,877
• Joined: 05-May 12

## 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.

### #3 alexb9054

Reputation: 0
• Posts: 4
• Joined: 05-October 12

## 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 Skydiver

• Code herder

Reputation: 6063
• Posts: 20,877
• Joined: 05-May 12

## 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.