Recursively calculate the standard deviation

  • #1
anonim
40
2
TL;DR 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.
 
Technology news on Phys.org
  • #2
anonim said:
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.
 
  • Like
Likes jim mcnamara

Similar threads

Replies
22
Views
3K
Replies
42
Views
2K
Replies
2
Views
2K
Back
Top