# Thread: What's wrong with my Average Array program?

1. ## What's wrong with my Average Array program?

I made a program that calculates the average of an array, but I'm getting wrong answers. When I enter 10 9 8 7 6 5 4 3 2 1, the average is 4199211. Are there any errors in my program? Thanks

Code:
```#include <stdio.h>
double average (int ary[ ]);
int main (void)
{
double ave;
int ary[10];
int numbers;
printf("Enter 10 numbers: \n");
for (numbers = 0; numbers < 10; numbers++)
scanf("%d", &ary[numbers]);
ave = average(ary);
printf("Average : %d\n", average);

system ("pause");
return 0;
}

double average (int ary[ ])
{
int sum = 0;
int numbers;
for (numbers = 0; numbers < 10; ++numbers)
sum += ary[numbers];
return (sum / 10);
}```

2. You printed a function pointer (average) instead of ave. In fact, the ave variable is not needed since you can print the result of average(ary) directly.

By the way, instead of using integer division, you probably want to return (sum / 10.0).

3. Thanks, but since I changed the average to ave. I get the average as 0. What is wrong with it now? lol. Thanks for all your help btw.

4. This is a mistake which could cause the problem:
Originally Posted by special1zed
Code:
```       for (numbers = 0; numbers < 10; ++numbers)
sum += ary[numbers];
return (sum / 10);
}```
Because you pre-increment (++numbers), instead of post-incrementing (numbers++). So the real range was 1-10 and not 0-9.

5. Originally Posted by special1zed
Thanks, but since I changed the average to ave. I get the average as 0. What is wrong with it now?

Originally Posted by MK27
Because you pre-increment (++numbers), instead of post-incrementing (numbers++). So the real range was 1-10 and not 0-9.
No, that is not the problem. Pre-increment and post-increment have the same net effect when used as stand alone expressions.

6. I changed that and I'm still getting 0 as the average. Thanks for the help though. I also changed the 10 to 10.0 and nothing changed.

7. What is your current code?

EDIT:
Oh wait, I notice that you are printing the average with a %d format specifier. It should be %f.

8. That did the trick, thank you sooooo much again!

9. Originally Posted by laserlight
No, that is not the problem. Pre-increment and post-increment have the same net effect when used as stand alone expressions.
Oh yeah...it's "for" that performs the operation. I guess that was too much presumption by me.

10. well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space..so cin n cout would be da better choice
dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...

11. Originally Posted by salmanriaz
well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space.
In this case there is only integer input, so scanf is fine.

Originally Posted by salmanriaz
dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...
Not so easy, since this is in C.

12. Originally Posted by salmanriaz
well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space...
maybe you cannot. I still can

13. Originally Posted by salmanriaz
well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space..so cin n cout would be da better choice
dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...
I didn't know Ali G could program.

Sorry.