# Thread: Arrays and Pointers:Move the calculation of average, variance and standard deviation

1. ## Arrays and Pointers:Move the calculation of average, variance and standard deviation

Code:
```#include <stdio.h>
#include <math.h>
#define N 400
int main()
{
float a[N]={ 1.8, 4.3, 15.6, 0.2, 10.7, 1.2, 0.9, 5.0, 4.6, 6.8, 4.6,0.7, 5.2,
5.5, 4.5, 5.5, 3.8, 6.8, 5.0, 1.2, 2.0, 1.1, 5.0, 3.3, 2.1, 10.9, 1.4, 1.3,
0.1, 5.0, 2.6, 1.5, 10.3, 23.9, 1.3, 1.8, 1.8, 7.1, 3.6, 9.5, 9.2, 16.1,
15.0, 0.7, 10.0, 5.9, 1.6, 2.8, 2.7, 0.8, 0.2, 6.5, 7.4, 5.2, 0.6, 2.7,
14.8, 6.8, 9.8, 14.9, 5.1, 0.9, 1.3, 4.2, 3.5, 2.2, 1.2, 6.8, 9.0, 8.9, 5.7,
11.5, 1.6, 13.2, 1.1, 3.3, 4.7, 9.9, 1.5, 9.3, 2.6, 8.5, 13.0, 8.2, 1.5};
float sum=0, average, var=0, sd;
int i;
for (i=0;i<N;i++) sum=sum+a[i];
average=sum/N;
for (i=0;i<N;i++) var=var+pow( a[i]-average, 2);
sd=sqrt((var)/(N-1.0));
printf("Average= %f Variance=%f S.D.=%f\n", average, var, sd);
return 0;
}```
a) move the calculation of average, variance and standard deviation from main into a separate function. Set up the function to accept an array by its pointer (the address of the zeroth element). Test your modified program with a simple array specified when the array is initialized.
this my code
Code:
```((After the Array))
int i;
float *a;
a = &array[0];
float sum=0.0, average, var=0.0, sd;
printf("Average= %f Variance=%f S.D.=%f\n", average, var, sd);
return 0;
}

float average (int *(a+i), int N)
{
int i;
float sum = 0.0;
for (i=0;i<N;i++) sum = sum + *(a+i);
return (sum / N);
}
float var(int *(a+1), float average )
{
int i;
float sd;
for (i=0;i<N;i++) var = var + pow( *(a+i) - average, 2);
sd = sqrt((var)/(N - 1.0));
return (sd);
}```
Code:
``` error: expected ) before + token error: expected ) before + token
error: expected ;, , or ) before int
error: expected ) before + token
error: expected ;, , or ) before float```
Can some help me out with the errors

2. You should post the complete updated version of the code, so we can compile it ourselves. Additionally, make sure your code is neatly formatted and indented, both in your editor and on the forum. Your code (the updated version) should look something like this:

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

#define N 400

int main()
{
float array[N]={ 1.8, 4.3, 15.6, 0.2, 10.7, 1.2, 0.9, 5.0, 4.6, 6.8, 4.6,0.7, 5.2,
5.5, 4.5, 5.5, 3.8, 6.8, 5.0, 1.2, 2.0, 1.1, 5.0, 3.3, 2.1, 10.9, 1.4, 1.3,
0.1, 5.0, 2.6, 1.5, 10.3, 23.9, 1.3, 1.8, 1.8, 7.1, 3.6, 9.5, 9.2, 16.1,
15.0, 0.7, 10.0, 5.9, 1.6, 2.8, 2.7, 0.8, 0.2, 6.5, 7.4, 5.2, 0.6, 2.7,
14.8, 6.8, 9.8, 14.9, 5.1, 0.9, 1.3, 4.2, 3.5, 2.2, 1.2, 6.8, 9.0, 8.9, 5.7,
11.5, 1.6, 13.2, 1.1, 3.3, 4.7, 9.9, 1.5, 9.3, 2.6, 8.5, 13.0, 8.2, 1.5};
int i;
float *a;

a = &array[0];

float sum=0.0, average, var=0.0, sd;

printf("Average= %f Variance=%f S.D.=%f\n", average, var, sd);

return 0;
}

float average (int *(a+i), int N)
{
int i;
float sum = 0.0;

for (i=0;i<N;i++)
sum = sum + *(a+i);

return (sum / N);
}

float var(int *(a+1), float average )
{
int i;
float sd;

for (i=0;i<N;i++)
var = var + pow( *(a+i) - average, 2);

sd = sqrt((var)/(N - 1.0));

return (sd);
}```
You have some major issues with this code.

1. Your function arguments are wrong - I'm not sure what you think is happening here: "int *(a+1)". If you're passing an array, simply declare it as a pointer:

Code:
`float average (int *a, int n)`
There's also the issue that the only array in your program is of type "float", so "int *a" is of the wrong type.

2. You're not calling your "average()" and "var()" functions anywhere. You declare variables called "average" and "var" in main, and print them without giving them values, but that's not the same as calling functions.

I'd suggest you read up on functions in your learning material, to see how they're called and how to assign their return value to a variable. There's also a tutorial here.

3. If you're defining your function after "main()", you need to declare them before "main()". See post #8 here: Interesting Program - Snakes and Ladders

The deeper I dig, the more I see a lot of other problems in there (both small and large). I'd recommend starting over, and building your program gradually - write a bit of code, compile, test. Only do a little bit at a time, and make sure it works before adding more code. See here: A development process

Code:
`float average(int *a, int N)`
Code:
`float var(int *a, float average)`
2. var is a function name and its address, so you cannot assign to it any value.
Code:
`for (i=0;i<N;i++) var = var + pow( *(a+i) - average, 2);`
3. Use indentations.

4. Originally Posted by DRK
`float average(int *a, int N)`
`float var(int *a, float average)`