# Need help with finding the average and standard deviation of an array

This is a discussion on Need help with finding the average and standard deviation of an array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> int average(int population[], int count) { int i = 0; int ...

1. ## Need help with finding the average and standard deviation of an array

Code:
```#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int average(int population[], int count) {

int i = 0;
int sumofarray = 0;

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

sumofarray += population[i];
}

return sumofarray / count ;
}

int stddev(int population[], int count, int averageofarray) {

int difference = 0;
int differencesquared = 0;
int sumofdifferencesquared = 0;
int averageofdifferencesquared = 0;
int i = 0;
for (i=0; i<100; i++);
{
difference = population[i] - averageofarray;

differencesquared = difference * difference;

sumofdifferencesquared += differencesquared;

averageofdifferencesquared = sumofdifferencesquared / count;

}
return sqrt(averageofdifferencesquared);
}

main()
{

int count = 0;
int i = 0;

int averageofarray = 0;
int standarddeviation = 0;

int population[100];

for (i=0; i<100; i++)
{
population[i] = rand()%100;

count++;
}

averageofarray = average(population, count);

standarddeviation = stddev(population, count, averageofarray);

printf("The array you entered consists of these numbers:\n");
for(i=0;i<100;printf("%d ",population[i]), i++);

printf("\nThe average of the population is %d\n", averageofarray);
printf("The standard deviation of the population is %d", standarddeviation);

return 0;
}```
I have to use a function for average and a function of standard deviation, which each call the randomly generated array and spit out their values to the prints. I'm not supposed to use pointers and am just getting stuck somewhere and this is due tomorrow morning. Any help would be greatly appreciated since I'm really new to C and programming in general.

Not getting an accurate average now.

2. Just pass the name of the array. Not an array + index.

Quzah.

3. Awesome now the program is running almost perfectly. I'm getting an average of 1 though, which can't be correct anything else you see involving the average resulting in 1 and not 50ish would be amazing. Thank you so much for your quick response you are saving me from a sleepless night.

4. Code:
`averageofarray = (int) average(population[100], count);`
Your function returns an int, and you are assigning it to an int, so you don't need a cast. (You almost never need to cast in C, if you do, you're probably doing something wrong.)
Code:
```int average(int population[], int count) {

int i = 0;
int sumofarray = 0;

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

sumofarray += population[i];
}

return sumofarray / count ;
}```
I'm not really sure why you have count, since everything you do is based off of 100.

Quzah.

5. Yea I'm not sure what I was messing with there, but I edited that out moments before you re-posted the average is all that is troubling me now. Oh and the count was just a specified part of the program I know its not necessary since there isn't any user input to the array. I was getting an accurate average when I put the sumofarray into main so I'm not sure why it isn't working in the average function.

6. Originally Posted by 001bob
I'm not supposed to use pointers
Technically this:
Code:
`void foo( int bar[] );`
Is a pointer. Any time you pass an array to a function it is actually passed as a pointer to the type of the first element. So that is actually:
Code:
`void foo( int *bar );`
If you're bored: Arrays and Pointers

Quzah.

7. Oh yea well calling by reference is okay for the arrays, which I understand I just meant for the rest of it besides the population array elements in case someone was suggesting making a pointer out of something else for convenience sake. No asterisks *ptr basically. I tinkered around with the count and sums to try and figure it out, but I'm still stumped with this average thing. Thanks for the literature on arrays and pointer its a good overview.