Objective Given a list of N integers, find its en it se ARES e aRENE Nwt; ese 3
ID: 3589045 • Letter: O
Question
Objective Given a list of N integers, find its en it se ARES e aRENE Nwt; ese 3 root mean square and standard deviation. Your program will first ask for N, the number of ntegers in the list, which the user will input. Then the user will input N more numbers. You need to use an integer array to store all the numbers. You also need to use separate functions for each calculation. egor n h a arih Note: Please consult wikipedia for root-mean-square and standard deviation Input Format: Your program will loop on user inputs and print out the statistics until user stops by entering 0 integers Output Format: Try your imagination.Explanation / Answer
using System;
using System.Collections.Generic;
namespace SampleApp
{
internal class Program
{
private static void Main()
{
int n;
Console.WriteLine("Enter the how many integers do you want to read");
n=Convert.ToInt32(Console.ReadLine());
double[] numbers=new double[n];
Console.WriteLine("Enter the numbers");
for(int i=0;i<n;i++)
{
numbers[i]=Convert.ToDouble(Console.ReadLine());
}
List<double> data = new List<double>();
for(int i=0;i<n;i++)
{
data.Add(numbers[i]);
}
double mean = data.Mean();
double variance = data.Variance();
double sd = data.StandardDeviation();
Console.WriteLine("Square Root of Mean is "+ Math.Sqrt(mean));
Console.WriteLine("Standard Deviation is "+sd);
}
}
public static class MyListExtensions
{
public static double Mean(this List<double> values)
{
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
}
public static double Mean(this List<double> values, int start, int end)
{
double s = 0;
for (int i = start; i < end; i++)
{
s += values[i];
}
return s / (end - start);
}
public static double Variance(this List<double> values)
{
return values.Variance(values.Mean(), 0, values.Count);
}
public static double Variance(this List<double> values, double mean)
{
return values.Variance(mean, 0, values.Count);
}
public static double Variance(this List<double> values, double mean, int start, int end)
{
double variance = 0;
for (int i = start; i < end; i++)
{
variance += Math.Pow((values[i] - mean), 2);
}
int n = end - start;
if (start > 0) n -= 1;
return variance / (n);
}
public static double StandardDeviation(this List<double> values)
{
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
}
public static double StandardDeviation(this List<double> values, int start, int end)
{
double mean = values.Mean(start, end);
double variance = values.Variance(mean, start, end);
return Math.Sqrt(variance);
}
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.