# Question about getting average of an array in for loop

Page 1 of 1

## 3 Replies - 1363 Views - Last Post: 07 August 2009 - 07:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=118936&amp;s=07af039cba142beeb4707ea3d0227a7f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 diturralde

Reputation: 0
• Posts: 2
• Joined: 06-August 09

# Question about getting average of an array in for loop

Posted 06 August 2009 - 09:24 PM

Hi,

I'm just learning C and have a doubt in a problem, here it is:

```#include <stdio.h>

int main(){

float ave = 0.00f, ave_a = 0.00f, ave_f = 0.00f;
int s = 0, s1 = 0, s2 = 0;

for (i = 0; i < 4; i++){

}

for (i = 0; i < 4; i++){
if (grades[i] > 13){ //This is the min to pass.
s += 1;
ave_a = (ave_a + grades[i]) / s;
}else{
s1 += 1;
ave_f = (ave_f + grades[i]) / s1;
}

s2 += 1;
ave = (ave + grades[i]) / s2; //this is the problem....not getting correct average
}

printf("You passed %d grades: \n", s);
printf("You failed %d grades: \n", s1);
printf("The average of the grades passed is: %.2f\n", ave_a);
printf("The average of the grades failed is: %.2f\n", ave_f);
printf("The average of all the grades is: %.2f\n", ave);

getchar();
return 0;
}

```

Output:

15
18
11
9
The average of the grades passed is: 16.50
The average of the grades failed is: 10.00
The average of all the grades is: 4.54

My question is why can't I get the average of all the elements in the array as I'm doing within the if, I get a wrong average. I doing it within the loop, it should get me a correct average???

Is This A Good Question/Topic? 0

## Replies To: Question about getting average of an array in for loop

### #2 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Question about getting average of an array in for loop

Posted 06 August 2009 - 10:29 PM

diturralde, on 7 Aug, 2009 - 03:24 AM, said:

Hi,

I'm just learning C and have a doubt in a problem, here it is:

Output:

15
18
11
9
The average of the grades passed is: 16.50
The average of the grades failed is: 10.00
The average of all the grades is: 4.54

My question is why can't I get the average of all the elements in the array as I'm doing within the if, I get a wrong average. I doing it within the loop, it should get me a correct average???

you a dividing within the loop (are you trying to calculate a running average?)
```		s2 += 1;
ave = (ave + grades[i]) / s2; //this is the problem....not getting correct average
}

```

for a normal average sum within the loop and divide when you have finished, e.g.
```		s2 += 1;
ave = (ave + grades[i]); //this is the problem....not getting correct average
}

printf("The average of all the grades is: %.2f\n", ave/s2);  // divide here!

```

### #3 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Question about getting average of an array in for loop

Posted 07 August 2009 - 01:02 AM

```int size = 10;
int myArray[size];
int average = 0;
for{int i=0; i<size; i++)
average += myArray[i];
average/=size;

```

how to find the average of an array

This post has been edited by ImaSexy: 07 August 2009 - 01:02 AM

### #4 diturralde

Reputation: 0
• Posts: 2
• Joined: 06-August 09

## Re: Question about getting average of an array in for loop

Posted 07 August 2009 - 07:42 AM

got it.

thanks everyone for the help...