using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Statistiacal_Analysis
{
class data
{
//Function for Mean-----------------------------------------------------------------------------------------
public int mean(int[] arr, int num)
{
int me = 0;
for (int i = 0; i < num; i++)
{
me = me + arr[i];
}
me = me / num;
return me;
}
//------------------------------------------------------------------------------------------------------------------
//Function for Median----------------------------------------------------------------------------------------
public void median(int[] arr, int num)
{
int me = 0;
// For Even Size Of The Array----------------------------------------------------------------------------
if (num % 2 == 0)
{
me = ((arr[(num / 2) - 1]) + (arr[num / 2])) / 2;
Console.WriteLine("The Median Is : " + me);
Console.WriteLine("=======================================================");
}
// For Odd Size Of The Array---------------------------------------------------------------------------
else
{
me = arr[((num + 1) / 2) - 1];
Console.WriteLine("The Median is : " + me);
Console.WriteLine("=======================================================");
}
}
//-----------------------------------------------------------------------------------------------------------------
//Function for Mode-------------------------------------------------------------------------------------
public void mode(int[] array, int size)
{
int counter = 1;
int max = 0;
int Mode = array[0];
for (int pass = 0; pass < size - 1; pass++)
{
if (array[pass] == array[pass + 1])
{
counter++;
if (counter > max)
{
max = counter;
Mode = array[pass];
}
}
else
counter = 1; // reset counter.
}
Console.WriteLine("The mode is: " + Mode);
Console.WriteLine("========================================================");
}
//------------------------------------------------------------------------------------------------------------------
//Function for Standerd Deviation--------------------------------------------------------------------------
public void stanDev(int[] arr, int num)
{
double sd = 0.0;
double power = 2.0;
double val = 0.0;
int Mean = mean(arr, num);
for (int i = 0; i < num; i++)
{
val = (arr[i] - Mean);
sd = sd + (Math.Pow(val, power)) / (num - 1);
}
Console.WriteLine("The Standerd Deviation Is : " + (Math.Sqrt(sd)));
Console.WriteLine("=======================================================");
}
//-----------------------------------------------------------------------------------------------------------------
//Function For Printing Histogram---------------------------------------------------------------------------
public void histogram(int[] arr, int num)
{
string[][] a = new string[num][];
for (int i = 0; i < num; i++)
{
a[i] = new string[arr[i]];
}
Console.Write("Element\t\tValue\t\tHistogram\n");
Console.WriteLine();
for (int i = 0; i < num; i++)
{
Console.Write(i + "\t\t" + arr[i] + "\t\t");
for (int j = 0; j < arr[i]; j++)
{
if (j % 4 == 0 && j != 0)
{
a[i][j] += "|";
}
else
{
a[i][j] += "*";
}
Console.Write(a[i][j]);
}
Console.WriteLine();
}
}
//-----------------------------------------------------------------------------------------------------------------
}
}
//------------------------------------------------- Class data ends here---------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Statistiacal_Analysis
{
class Program
{
//DRIVER METHOD--------------------------------------------------------------------------------------
static void Main(string[] args)
{
data z = new data();
string s="";
// Declarations Of Variables -----------------------------------------------------------------------
Random r = new Random();
int a=0;
// Dynamically Generated Size In Array-------------------------------------------------------------
Console.WriteLine("=======================================================");
Console.WriteLine("\t\t\t STAISTICAL DATA ANALYSIS");
Console.WriteLine("=======================================================");
Console.Write("Enter The Size OF The Array : ");
a = int.Parse(Console.ReadLine());
Console.WriteLine("-------------------------------------------------------------------------------");
int []N=new int[a];
// Random Numbers Assigned To The Array-----------------------------------------------------------
for (int i = 0; i < a; i++)
{
N[i] = r.Next(0, 9);
Console.WriteLine("The Collected Sample # "+ i +" Is : " + N[i]);
}
Console.WriteLine("=======================================================");
for (int i = 0; i < a; i++)
{
//Sorting The Array In Order To Calculate Mode---------------------------------------------------
Array.Sort(N);
//Printing The Sorted Array----------------------------------------------------------------------------
Console.WriteLine("The Sorted Data Is : " + N[i]);
}
//---------------------------------------------------------------------------------------------------------------
//function call for Mean------------------------------------------------------------------------------------
Console.WriteLine("=======================================================");
Console.WriteLine("The Mean Is : "+z.mean(N,a));
Console.WriteLine("=====================================================");
//---------------------------------------------------------------------------------------------------------------
//function call for Median----------------------------------------------------------------------------------
z.median(N,a);
//--------------------------------------------------------------------------------------------------------------
//function call for Mode------------------------------------------------------------------------------------
z.mode(N, a);
//--------------------------------------------------------------------------------------------------------------
//function call for Standerd Deviation-------------------------------------------------------------------
z. stanDev(N, a);
//---------------------------------------------------------------------------------------------------------------
//function call for Histogram-------------------------------------------------------------------------------
Console.Write("FOR HISTOGRAM PRESS H OR TO CLOSE PRESS ENTER : ");
s=Console.ReadLine();
if (s == "h")
{
z.histogram(N, a);
}
else
{
Environment.Exit(0);
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment