The standard deviation of a measure of the spread of the dat
The standard deviation of a measure of the spread of the data with respect to the mean (average). Data with a small standard deviation will be clustered close to the mean and data with larger standard deviation will be more spread out. To compute the standard deviation, find the mean, find the difference of each item from the mean, square those differences, find the average of those squares, and, finally, find the square root of that average, which is the standard deviation. For example, given the data 10, 20, 30,
Mean (10+20+30)/3=20
Differences (10-20) = 10 (10-10) = 0 (30-20) = 10
Squares of differences 100, 0, 100
Average of squares (100+0+100) / 3 =66.7
Square root of the average 8.2
Write a C# program to compute the mean and the standard deviation of the elements in an array with elements of type double.
No Java programs allowed you must write in the C# language
using System; 10 public class Reverse Input Array private int data the array to reverse 11 12 13 public Reverse Input Array int d) data di 14 15 16 Uses input dialogs to input an array 17 18 public static int ReadIntArray() Console. Write(\"Enter the array size: 19 int size int. Parse (Console.ReadLine 20 int anArray new int size]; 21 for (int 1-0 i size; i++){ 22 Console .Write Enter anArray +1+ j anArray[i] int Parse (Console.ReadLine 24 return anArray 26 27 28 Swaps the array elements at indices left and right 29 30 public void swap (int left int right) int temp data left] 31 data left] data right] 32 data right] tempi 34 36 E Reverses the order of the array elements between indices left and right 38Solution
Solution ::
// pre-processor directives //
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int p;
float mean,med,std;
Console.Write(\"Enter number of data points:\");
p = int.Parse(Console.ReadLine());
if (p < 3)
{
Console.WriteLine(\"The number of data points should be greater than 2.\");
}
else
{
//declare an array of p size to store integral data points
int[] data_set = new int[p];
//allow user inputs
int m = 0;
for (m = 0; m < p; m++)
{
Console.Write(\"[{0}]:\", m);
data_set[m] = int.Parse(Console.ReadLine());
}
//sorting the data set
bubblesort(data_set, p);
//code for calculating mean
int sum = 0;
int n = 0;
while (n < p)
{
sum = sum + data_set[n];
n++;
}
mean = (float)sum / p;
// calculate median
// If p is odd, then med=data_set[p/2]
// If p is even,then med=(data_set[p/2]+data_set[1+p/2])/2
//The index of array starts from the 0
if (p % 2 != 0) med = data_set[p / 2];
else med = (data_set[(p / 2) - 1] + data_set[p / 2]) / (float)2;
//calculate the mode
int[,] mode = new int[p, 2];
//initialize 2D array storing numbers of occurences, and values
for (m = 0; m < 2; m++)
for (n = 0; n < p; n++) mode[n, m] = 0;
mode[0, 0] = 1;
for (m = 0; m < p; m++)
for (n = 0; n < p - 1; n++)
if (data_set[m] == data_set[n + 1]) { ++mode[m, 0]; mode[m, 1] = data_set[m]; }
int max;
int k = 0;
max = mode[0, 0];
for (n = 0; n < p; n++)
if (max < mode[n, 0]) { max = mode[n, 0]; k = n; }
//Code for calculating standard deviation
float temp = 0.0f;
for (n = 0; n < p; n++)
{
temp = temp + (float)Math.Pow(data_set[n] - mean, 2);
}
std = (float)Math.Sqrt(temp / (p - 1));
//Displaying Results
Console.WriteLine(\"Statistical Information:\");
Console.WriteLine(\"Arithmetic mean:{0}\", mean);
Console.WriteLine(\"med:{0}\", med);
if (mode[k, 1] != 0)
Console.WriteLine(\"Mode:{0}\", mode[k, 1]);
else Console.WriteLine(\"Mode: no mode\");
Console.WriteLine(\"Standard_Deviation of set {0}\", std);
}
Console.ReadLine();
}
///bubble sort
static void bubblesort(int[] data_set, int p)
{
int m, n;
for (m = 0; m < p; m++)
for (n = p - 1; n > m; n--)
if (data_set[n] < data_set[n - 1])
{
int temp = data_set[n];
data_set[n] = data_set[n - 1];
data_set[n - 1] = temp;
}
}
}
}
/// *** Thank You *** ///


