Recursively calculate the standard deviation

  • #1
29
2

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.
 

Answers and Replies

  • #2
33,646
5,315
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

Related Threads on Recursively calculate the standard deviation

Replies
39
Views
2K
  • Last Post
Replies
8
Views
349
  • Last Post
Replies
12
Views
80K
Replies
4
Views
9K
  • Last Post
Replies
8
Views
838
  • Last Post
Replies
5
Views
4K
  • Last Post
Replies
5
Views
816
  • Last Post
Replies
7
Views
4K
Replies
3
Views
617
Top