I'm currently trying to make a calculator that calculates Mean / Median / IQR / Standard Deviation / Variance of a set of data and a set of frequencies. To get the Mean of a set of data, i had intended to use the Sum() function and Lambda expressions, however i ran into a problem with this code below.
public double stat_getMeanGrouped(double[] dataset, double[] frequency)
{
return dataset.Sum(x => x * frequency[int.Parse(x.toString)] / (double)frequency.Sum();
}
stat_getMeanGrouped(new double[] {1,2,3}, new double[] {1,1,1});
should return 2
however returns an index outside of range exception.
I understand why the error is happening, i just need help amending it.
int.Parse(x.toString)?! Eeeehm, what? Just cast to int:(int)x.datasetis. You're using it to accessfrequenciesby index, however indeices start at zero instead of one. So the first element isfrequencies[0]. Maybe you just need to substract one fromx?