The following code takes an array of integers and create an array with the mobile means (i.e. the value in i-th place is the mean of the last n elements in the array before i (if they exists); if i<n, it is the mean of the existing elements before i. Thus as an example: for n=3 1 2 3 4 5 6 -> 1 1.5 2 3 4 5
#include <stdio.h>
#include <stdlib.h>
float *
mobMean (int x[], int n, int nval)
{
int i, j, sum, num;
float *means;
if(means=malloc(sizeof(float) * nval))
{
for(i=0; i<nval; i++)
{
sum=0;
num=0;
for(j=i; j>=0 && i-j>=n; j--)
{
sum+=x[j];
num++;
}
*(means+i)=(float)sum/num;
}
}
else
printf("e");
return means;
}
int
main()
{
int a[10], i;
float *b;
for(i=0; i<10; i++)
scanf("%d", &a[i]);
b=mobMean(a,3,10);
for(i=0; i<10; i++)
printf("%f", *(b+i));
free(b);
return 0;
}
The console (gcc compiler) returns as an output -nan 10 times consecutively. not only my pc, but also the online compiler. google doesn't have a clue of what that is. i would really appreciate your help.