This program calculates the standard deviation of 10 data using arrays.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Program { private static double CalculateSD(double[] numbers) { double sum = 0, mean, sd = 0; for (int i = 0; i < numbers.Length; ++i) { sum += numbers[i]; } mean = sum / numbers.Length; for (int i = 0; i < numbers.Length; ++i) sd += Math.Pow(numbers[i] - mean, 2); return Math.Sqrt(sd / numbers.Length); } static void Main(string[] args) { double[] data= new double[5]; Console.WriteLine("*** Enter 10 elements ***"); for (int i = 0; i < data.Length; ++i) data[i] = Convert.ToSingle(Console.ReadLine()); Console.WriteLine($"{CalculateSD(data):0.00##}"); Console.ReadLine(); } } |
Output: