static void Main(string[] args)
{
int[] Temp = new int[5];
Random num = new Random();
for (int i = 0; i < Temp.Length; i++)
{
Temp[i] = num.Next(10, 50);
Console.WriteLine(Temp[i]);
}
Console.ReadLine();
{
double total = 0;
foreach (int i in Temp)
total += i;
double avg = total / Temp.Length;
Console.WriteLine("Average = {0}\n", avg);
Console.ReadLine();
double sumofsquares = 0;
double Deviation = 0;
sumofsquares += Math.Pow(total, 2);
Deviation = Math.Sqrt((sumofsquares - Math.Pow(total, 2) / Temp.Length) / (Temp.Length - 1));
Console.WriteLine("The Standard Deviation is {0}", Deviation);
Console.ReadLine();
}
}
}
}
Standard Deviation
Page 1 of 11 Replies - 158 Views - Last Post: 06 February 2012 - 07:25 PM
Topic Sponsor:
#1
Standard Deviation
Posted 06 February 2012 - 07:06 PM
Hi, I am writing a program that takes an array of 5 and calculates the average and standard deviation, I have the average part right, I just can't find a proper algorithm to correctly calculate the deviation. Any help with my math or code would be greatly appreciated, I've been searching for the answer, but any tutorials are either really vague or do not work correctly in my program. Here's the best I could come up with so far:
Replies To: Standard Deviation
#2
Re: Standard Deviation
Posted 06 February 2012 - 07:25 PM
Here's a basic write up, read it and try to fit it to what you are trying to accomplish.
var numbers = new List<int>(){ 1, 3, 4, 6, 7, 8 };
var total = 0;
foreach(var num in numbers) {
total += num;
}
var mean = total / numbers.count();
var computedNumbers = new List<int>();
foreach(var num in numbers) {
computedNumbers.add(SquareIt(num - 2));
}
var averageTotal = 0;
foreach(var num in computedNumbers) {
averageTotal += num;
}
var squareRootValue = Math.Sqrt((averageTotal / computedNumbers));
Console.WriteLine(squareRootValue);
/******************************/
public int SquareIt(int number) {
return number * number;
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|