How to resolve the algorithm Cumulative standard deviation step by step in the C# programming language
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
The provided C# program calculates and prints the standard deviation of a list of numbers for each subset of the original list containing i elements.
-
It uses the
GetRange
method to create a new list with a specified range of elements from the original list. -
The
sdev
function is used to calculate the standard deviation of the list passed to it. -
A new list
store
is created to store the squared differences between each element of the input list and the mean of that list. -
The
Math.Sqrt
method is used to calculate the square root of the mean of the squared differences, which gives the standard deviation.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
using System.Linq;
namespace standardDeviation
{
class Program
{
static void Main(string[] args)
{
List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
for (int i = 1; i <= nums.Count; i++)
Console.WriteLine(sdev(nums.GetRange(0, i)));
}
static double sdev(List<double> nums)
{
List<double> store = new List<double>();
foreach (double n in nums)
store.Add((n - nums.Average()) * (n - nums.Average()));
return Math.Sqrt(store.Sum() / store.Count);
}
}
}
You may also check:How to resolve the algorithm Halt and catch fire step by step in the 11l programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the EasyLang programming language