Recursively calculate the standard deviation

Click For Summary
SUMMARY

The forum discussion centers on a C program intended to calculate the standard deviation of a series of integers. The user attempts to implement the calculation using a recursive function named foo, but the logic is flawed. The correct formula for standard deviation involves calculating the variance first, which is not properly addressed in the provided code. The expected output for an input of 6 should be approximately 1.870829, not 2.100528 as stated.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of recursion in programming
  • Knowledge of mathematical concepts related to standard deviation
  • Familiarity with the math.h library for mathematical functions
NEXT STEPS
  • Review the correct formula for calculating standard deviation, focusing on variance computation
  • Learn about iterative versus recursive approaches in C programming
  • Explore debugging techniques for C programs to identify logical errors
  • Investigate the use of arrays in C for storing and processing multiple values
USEFUL FOR

C programmers, students learning about statistics, and anyone interested in implementing mathematical algorithms in code.

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   Reactions: jim mcnamara

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 42 ·
2
Replies
42
Views
6K
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
2K