Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project: Statistics Calculation This project involves working with a set of disc

ID: 3749923 • Letter: P

Question

Project: Statistics Calculation This project involves working with a set of discrete data and determining their average (arithmetic mean), median, mode, and range. In brief, these operations are explained below. You should sort the set before any of this operation. Given a set of the following discrete numbers/grades: 980, 55, 200, 34, 112, 55, 87, 256, 100, 100 1) The "average" or "arithmetic mean" of the grades is the sum of all grades divided by the number of the grades. (9e0, 55, 280, 34, 112, 55, 87, 256, 100, 10e) - 190.9 10 The "median" is the value located in middle of the sequence. If the number of values is odd, the middle value is the median, but if the number of values is even, the average of the two middle values is the median. Please note that the number must be sorted before this operation. The sorted sequence becomes: 34, 55, 55, 87, 100, 100, 112, 200, 256, 900. Since the number of values here is even, the median is the average of the two middle numbers. 2) (1ee + 100) median--180 The "mode" is the values with more than one frequency. If the set doesn't feature a frequency, then there is no mode in the sequence. For a value to be considered, it must occur more than once. If there are values with the same frequency, those values are said to be the mode 3) Given the sequence: 34, 55, 55, 87, 100, 100, 112, 200, 256, 980. We have two numbers with the same frequencies and the are 55 and 180 The "range" is the difference between the highest and lowest values. The highest grade in our example is 980 and the lowest is 34. 4) range 9 34 -866 Requirements This is an Object Oriented Programming (00P) problem; therefore, you should solve the problem using classes You should read the data from the input file provided along with this project Your code must be properly documented and formatted.

Explanation / Answer

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stats
{
class Program
{
static void Main(string[] args)
{
//Replace the input file path with the given input file path, file path can also be read from the console
string filePath = @"C:UsersQP131Desktopinput.txt";
//assuming that the file contains the numbers as comma separated like 12,20,4,6... in case it is space separated just use ' ' instead of ',' in split function
string[] splittedNumbers = File.ReadAllText(filePath).Split(',');

List<double> numbers = new List<double>();

//converting string numbers to actual numbers
foreach (var num in splittedNumbers)
{
numbers.Add(Convert.ToDouble(num));
}

Console.WriteLine("Input data set : " + string.Join(", ", numbers));

// created new instance of the calculator class passing the list of numbers
StatisticsCalculator calculator = new StatisticsCalculator(numbers);

Console.WriteLine();
Console.WriteLine("Average of the data set : "+calculator.GetAverage() );
Console.WriteLine("Median of the data set : "+calculator.GetMedian() );

var mode = calculator.GetMode();
// handing the case where no mode exists
if(mode == null)
Console.WriteLine("Mode of the data set : No mode exist" );
else
Console.WriteLine("Mode of the data set : "+ string.Join( ", ",mode)); // in case of mutiple modes showing as comma separated values

Console.WriteLine("Range of the data set : "+calculator.GetRange() );

Console.ReadLine();
}
}
//class for all the calculations
public class StatisticsCalculator
{
public StatisticsCalculator(List<double> numbers)
{
Numbers = numbers;
Numbers.Sort();
NumbersCount = numbers.Count;
}

public List<double> Numbers { get; set; }
public int NumbersCount { get; set; }

public double GetAverage()
{
var sum = Numbers.Sum();

return sum / NumbersCount;
}

public double GetMedian()
{
int middleIndex = NumbersCount / 2;
if(NumbersCount%2 != 0) // in this case only one middle number will be there
{
return Numbers[middleIndex];
}
else
{
return (Numbers[middleIndex-1] + Numbers[middleIndex]) / 2;// in this case two middle numbers will be there hence taking the average of the two
}
}

public List<double> GetMode()
{
Dictionary<double, int> frequncy = new Dictionary<double, int>();
// calculating frequency of each different numbers
foreach (var num in Numbers)
{
if (!frequncy.Keys.Contains(num))
frequncy.Add(num,0);
frequncy[num] += 1;
}

List<double> result = new List<double>();
int maxFrequency = frequncy.Values.Max();
  
//Case where no repeating numbers
if (maxFrequency == 1)
return null;

//finding all unique values with max frequency
foreach (var key in frequncy.Keys)
{
if (frequncy[key] == maxFrequency)
result.Add(key);
}

return result;

}

public double GetRange()
{
double largestNumber = Numbers.LastOrDefault(); // last or default is used to handle the case of no any numbers in the list
double smallestNumber = Numbers.FirstOrDefault();
double range = largestNumber - smallestNumber;
return range;
}
}
}