Recursively calculate the standard deviation

Click For Summary
The discussion revolves around a code snippet intended to calculate the standard deviation, but it is noted that the implementation is incorrect. The provided code attempts to calculate the mean and uses a recursive function to compute a value based on the input number. However, the calculation method does not align with the standard deviation formula. The example given suggests that for an input of 6, the expected output should be approximately 2.100528, but the code fails to produce the correct result. The thread is ultimately closed, indicating that it was part of a homework-related inquiry.
anonim
Messages
39
Reaction score
2
TL;DR
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
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
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
Replies
7
Views
2K
  • · Replies 42 ·
2
Replies
42
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K