C# exercises and solutions-C# array: statistical information


C# array exercise: statistical information

Exercise : Write a C# program to answer about the statistical information such as arithmetic mean, median, mode, and standard deviation of an integer data set. The data points are input by the user from keyboard. This program will display the output similar to the one shown below:

C# program to answer about statistical information  

If you are not sure about statistical information such as arithmetic mean, median, mode, and standard deviation, you will need to read this page:

Descriptive Measures  

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
  int n;
  float mean,median,std;
  Console.Write("Enter number of data points:");
  n = int.Parse(Console.ReadLine());
  if (n < 3)
   {
    Console.WriteLine("The number of data points should be greater than 2.");

    }
  else
  {

    //declare an array of n size to store integral data points
    int[] dataset = new int[n];
   //allow user inputs
   int i = 0;
   for (i = 0; i < n; i++)
    {
      Console.Write("[{0}]:", i);
      dataset[i] = int.Parse(Console.ReadLine());
    }

   //sort the data set
   bubblesort(dataset, n);


  //calculate the mean
   int sum = 0;
   int j = 0;
   while (j < n)
  {
     sum = sum + dataset[j];
     j++;
   }

   mean = (float)sum / n;

  //calculate median
  //If n is odd, median=dataset[n/2]
  //If n is even, median=(dataset[n/2]+dataset[1+n/2])/2
  //The index of array starts from 0, so you need to subtract 1 from the           //indices   used in calculating the median
  if (n % 2 != 0) median = dataset[n / 2];
  else median = (dataset[(n / 2) - 1] + dataset[n / 2]) / (float)2;

   //calculate the mode
   int[,] mode = new int[n, 2];
  //initialize 2D array storing numbers of occurences, and values
  for (i = 0; i < 2; i++)
  for (j = 0; j < n; j++) mode[j, i] = 0;
  mode[0, 0] = 1;

  for (i = 0; i < n; i++)
    for (j = 0; j < n - 1; j++)
      if (dataset[i] == dataset[j + 1]) { ++mode[i, 0]; mode[i, 1] = dataset[i]; }

   int max;
   int k = 0;
   max = mode[0, 0];
   for (j = 0; j < n; j++)
     if (max < mode[j, 0]) { max = mode[j, 0]; k = j; }


   //calculate standard deviation, std
   float temp = 0.0f;

   for (j = 0; j < n; j++)
  {
     temp = temp + (float)Math.Pow(dataset[j] - mean, 2);
   }

   std = (float)Math.Sqrt(temp / (n - 1));

   //Show results

   Console.WriteLine("Statistical Information:");
   Console.WriteLine("..................................................");
   Console.WriteLine("Arithmetic mean:{0}", mean);
   Console.WriteLine("Median:{0}", median);
   if (mode[k, 1] != 0)
     Console.WriteLine("Mode:{0}", mode[k, 1]);
   else Console.WriteLine("Mode: no mode");
   Console.WriteLine("Standard deviation:{0}", std);


 }
   Console.ReadLine();

}

///bubble sort

static void bubblesort(int[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
  for (j = n - 1; j > i; j--)
    if (dataset[j] < dataset[j - 1])
      {
         int temp = dataset[j];
         dataset[j] = dataset[j - 1];
         dataset[j - 1] = temp;
  }

}
}
}



Comments

Zet comment

 Zet

In order to count Mode you can use LINQ:

var mode1value = data.GroupBy(p => p).OrderByDescending(q => q.Count()).First().Key;
var mode1count = data.GroupBy(p => p).OrderByDescending(q => q.Count()).First().Count();
var mode2value = data.GroupBy(p => p).OrderByDescending(q => q.Count()).Skip(1).First().Key; //not working for two elements
var mode2count = data.GroupBy(p => p).OrderByDescending(q => q.Count()).Skip(1).First().Count();
Console.WriteLine($"Mode:\t\t {(mode1count==mode2count?"No mode":mode1value.ToString())}");


2017-04-13
Team comment

 Team

I came across this page while trying to code statistical program just for practice though. I manage to arrange the values in the array, find the mean and median, but I am stuck with the mode function. I would like to ask what is your programming concept for finding the mode as I know that mode refers to a value that reappears the most times. Thank you.


2017-03-01
Team comment

 Team

I came across this page while trying to code statistical program just for practice though. I manage to arrange the values in the array, find the mean and median, but I am stuck with the mode function. I would like to ask what is your programming concept for finding the mode as I know that mode refers to a value that reappears the most times. Thank you.


2017-03-01
shrill comment

 shrill

//There s something wrong with standard deviation that I cant fix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace statisticalInformation
{
public class Program
{
public void Main(string[] args)
{
Console.Write("Enter number of data points : ");
int dataInput = Int32.Parse(Console.ReadLine());
int[] numbers = new int[dataInput];

for (int i = 0; i < dataInput; i++)
{
Console.Write("[" + i + "]:");
numbers[i] = Int32.Parse(Console.ReadLine());


}

Console.WriteLine("Statistical Information : ");
Console.WriteLine("........................................................");
float mean = arithmeticMean(numbers, dataInput);
Console.WriteLine("Arithmetic Mean : " + mean);
float median = mediann(numbers,dataInput);
Console.WriteLine("Median : " + median);
int mode = modee(numbers, dataInput);
Console.WriteLine("Mode:" + mode);
double sd = standardDeviation(numbers, dataInput);
Console.WriteLine("Standard Deviation : " + sd);
}

public double standardDeviation(int[] numbers, int length)
{
float average,summation=0;
double sd;
int sum = 0;
float[] newArray = new float[length];
for (int i = 0; i < length; i++)
{
sum += numbers[i];
}
average = (float)sum / length;
for (int i = 0; i < length; i++)
{
newArray[i] = (numbers[i] - average)* (numbers[i] - average);
summation += newArray[i];
}
sd = Math.Sqrt(summation);
return sd;
}
//To find the mode
public int modee(int[] numbers, int length)
{
//this has ntn to do with finding the mode. this is just finding the highest
//number in the array
int high = numbers.OrderByDescending(n => n).First();
//here first the numbers in the array are grouped by numbers, then they are arranged
//in descending order based on their count, and then the first element in the numbers
//is displayed by its key.
int mode = numbers.GroupBy(i => i).OrderByDescending(g => g.Count()).First().Key;
return mode;
}
public float arithmeticMean(int[] numbers, int length)
{

int sum = 0;
for (int i = 0; i < length; i++)
{
sum += numbers[i];
}
float mean = (float)sum / length;

//double mean = numbers.Average();
return mean;

}
public float mediann(int[] numbers, int length)
{
int[] newaray = bubblesort(numbers,length);
int midValue = length / 2;
float median;
if (length % 2 == 0)
{
median = (newaray[midValue] + newaray[midValue - 1])/2;
}
else
{
median = newaray[midValue];
}
return median;
}

//The numbers are sorted to find the median, this method is called by the median method
public int[] bubblesort(int[] numbers,int length)
{
for (int i = 0; i < length-1; i++)
{
for (int j = i + 1; j < length; j++)
{
int temp;
if (numbers[i] > numbers[j])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

return numbers;
}

}
}


2017-02-09
awesome comment

 awesome

awesome!!!


2015-02-22



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.