1 Replies - 158 Views - Last Post: 06 February 2012 - 07:25 PM Rate Topic: -----

Topic Sponsor:

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

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:
  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();
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Standard Deviation

#2 Sergio Tapia  Icon User is online

  • Is that a raincoat?
  • member icon

Reputation: 1012
  • View blog
  • Posts: 3,816
  • Joined: 27-January 10

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;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1