# Recursively calculate the standard deviation

## Summary:

Standard Deviation

## Main Question or Discussion Point

Code:
#include<stdio.h>
#include<math.h>
double foo(int n, int mean){
double square;
if(n==1){
return(1);
}
if(n!=0){
square=pow(n-mean,2);
return( (sqrt(square)+foo(n-1,mean))/(sqrt(n-1))  );
}

}
int main(){
int num;
double mean;
int i;
int sum=0;
printf("Enter the number: ");
scanf("%d",&num);
for(i=1; num>=i; i++){
sum=sum+i;
}
mean=(double)sum/num;
foo(num,mean);

printf(" %lf ",foo(num,mean));
}

I want to calculate standard deviation. For example if the input is 6, I want to do this: sqrt( (6+5+4+3+2+1)/(5) ) . If the input is 6, output is going 2.100528. My code does not work properly.

Related Programming and Computer Science News on Phys.org
Mark44
Mentor
Summary:: Standard Deviation

Code:
#include<stdio.h>
#include<math.h>
double foo(int n, int mean){
double square;
if(n==1){
return(1);
}
if(n!=0){
square=pow(n-mean,2);
return( (sqrt(square)+foo(n-1,mean))/(sqrt(n-1))  );
}

}
int main(){
int num;
double mean;
int i;
int sum=0;
printf("Enter the number: ");
scanf("%d",&num);
for(i=1; num>=i; i++){
sum=sum+i;
}
mean=(double)sum/num;
foo(num,mean);

printf(" %lf ",foo(num,mean));
}

I want to calculate standard deviation. For example if the input is 6, I want to do this: sqrt( (6+5+4+3+2+1)/(5) ) . If the input is 6, output is going 2.100528. My code does not work properly.
This is not how the standard deviation is calculated.

Since your post seems to be a continuation of the thread in the Homework section, I am closing this thread.

• jim mcnamara