Hello, I'm writing an application that computes the variance and average from a list of integers. Here is the assignment info:
Create a function that accepts a list of integers and computes the average and variance of the list.
The variance is computed using the following formula.
The list of integers is passed as a params array.
The function returns the variance as a double.
The average is passed back to the calling function as a reference to type double.
Test the function you created by calling it with a list of integer values. Output the variance and average computed by the function to the screen. This should be done in your Main code, not the function itself.
You may call the function using literal values. This means you do not need to read values in from the keyboard. I demonstrate doing this in my ParamArray video.
Inside your function you may use the array.Sum() and array.Average() methods.
(Worth 25 points) Now overload the function created in part 1 so that it accepts a list of double values. This can be added to the console application created in part 1. Test the function by calling it with a list of values of type double. Output the computed average and variance to the screen.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_2_ITM_225
{
class Program
{
static double Average(List<int> listvalues)
{
double variance = 0;
foreach (int value in listvalues)
{
variance += (intList[i] - Average) * (intList[i] - Average);
}
return (double)variance / listvalues.Count;
}
static void Main(string[] args)
{
List<int> intList = new List<int>();
string mystring = "";
do
{
Console.Write("Enter an integer (exit to finish): ");
mystring = Console.ReadLine();
if (mystring.ToLower() != "exit")
{
intList.Add(int.Parse(mystring));
}
else
{
break;
}
}
while (true);
Console.WriteLine();
for (int i = 0; i < intList.Count; i++)
{
Console.WriteLine("Value {0}: {1}", i + 1, intList[i]);
}
Console.WriteLine("\nAverage: {0}\nVariance: {1}", Average(intList), intList.Sum());
Console.ReadKey();
}
}
}
I'm not sure how to get the variance to use the intList[i] for the numbers it needs, how do I do that? And are there any other problems? Thanks!