The main problem is that return
is inside the loop, so it does the calculation in the first step and then exits the function next. Retrieving return
from loop
it only runs at the end when all of it has been executed.
This is because the code is very disorganized. It is difficult to understand what is happening. I did it to improve it too.
I did not check if the calculations are correct.
#include <stdio.h>
float media(int n, float* v) {
float s = 0;
for (int i = 0; i < n; i++) {
s += v[i];
}
return s / n;
}
float variancia(int n, float* v, float m) {
float s = 0;
for (int i = 0; i < n; i++) {
s += (v[i] - m) * (v[i] - m);
}
return s / n;
}
int main() {
float v[10];
for (int i = 0; i < 10; i++) {
printf("Digite um numero:\n");
scanf("%f", &v[i]);
}
float med = media(10, v);
float var = variancia(10, v, med);
printf("Media = %f Variancia = %f \n", med, var);
}
See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .