How to resolve the algorithm Cumulative standard deviation step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Cumulative standard deviation step by step in the C programming language

Table of Contents

Problem Statement

Write a stateful function, class, generator or co-routine that takes a series of floating point numbers, one at a time, and returns the running standard deviation of the series. The task implementation should use the most natural programming style of those listed for the function in the implementation language; the task must state which is being used. Do not apply Bessel's correction; the returned standard deviation should always be computed as if the sample seen so far is the entire population.

Use this to compute the standard deviation of this demonstration set,

{ 2 , 4 , 4 , 4 , 5 , 5 , 7 , 9 }

{\displaystyle {2,4,4,4,5,5,7,9}}

, which is

2

{\displaystyle 2}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cumulative standard deviation step by step in the C programming language

This code creates a StatObject to keep track of some statistics, in this case the standard deviation of a set of numbers, the stat_obj_value function returns the value of the statistic that was set when the StatObject was created (in this case the standard deviation).

The stat_object_add function takes a StatObject and a number and adds the number to the sum and sum of squares of the numbers that have been added to the StatObject so far, and returns the updated value of the statistic.

In the main function, the code creates a StatObject for the standard deviation, and then adds each of the numbers in the array v to the StatObject using the stat_object_add function.

After each number is added, the code prints the number and the updated standard deviation.

Finally, the code frees the StatObject using the FREE_STAT_OBJECT macro.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;

typedef struct stat_obj_struct {
   double sum, sum2;
   size_t num;
   Action action; 
} sStatObject, *StatObject;

StatObject NewStatObject( Action action )
{
  StatObject so;

  so = malloc(sizeof(sStatObject));
  so->sum = 0.0;
  so->sum2 = 0.0;
  so->num = 0;
  so->action = action;
  return so;
}
#define FREE_STAT_OBJECT(so) \
   free(so); so = NULL
double stat_obj_value(StatObject so, Action action)
{
  double num, mean, var, stddev;
    
  if (so->num == 0.0) return 0.0;
  num = so->num;
  if (action==COUNT) return num;
  mean = so->sum/num;
  if (action==MEAN) return mean;
  var = so->sum2/num - mean*mean;
  if (action==VAR) return var;
  stddev = sqrt(var);
  if (action==STDDEV) return stddev;
  return 0;
}

double stat_object_add(StatObject so, double v)
{
  so->num++;
  so->sum += v;
  so->sum2 += v*v;
  return stat_obj_value(so, so->action);
}


double v[] = { 2,4,4,4,5,5,7,9 };

int main()
{
  int i;
  StatObject so = NewStatObject( STDDEV );

  for(i=0; i < sizeof(v)/sizeof(double) ; i++)
    printf("val: %lf  std dev: %lf\n", v[i], stat_object_add(so, v[i]));

  FREE_STAT_OBJECT(so);
  return 0;
}


  

You may also check:How to resolve the algorithm Perfect numbers step by step in the Ruby programming language
You may also check:How to resolve the algorithm 100 doors step by step in the C programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the LaTeX programming language
You may also check:How to resolve the algorithm ABC problem step by step in the SequenceL programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Perl programming language