Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am writing a program that calculates standard deviation in randomly generated temperatures. I believe that my algorithm is right, however the code only takes the first number in the Temp array and subtracts it from the mean, and I can't figure out why it does not go through the whole array. I have tried a foreach loop but always get a cannot convert i from type int. Here's the code I have so far, and any help would be greatly appreciated:

C#
static void Main(string[] args)
        {
            int[] Temp = new int[4];
            Random num = new Random();
            for (int i = 0; i < Temp.Length; i++)
            {
                Temp[i] = num.Next(10, 50);
                Console.WriteLine(Temp[i]);
            }

            {


                double dev = 0;
                double devsum = 0;

                double total = 0;
                foreach (double i in Temp)
                    total += i;
                double avg = total / Temp.Length;
                Console.WriteLine("Average = {0}\n", avg);
                for (int i = 0; i < Temp.Length; i++)
                {
                    ///// For some reason it only takes the first i in Temp
                    ///// does not work for the other i's in Temp
                    dev = (Temp[i] - avg);
                    dev = Math.Pow(dev,2);
                    devsum = devsum + dev;
                    Console.WriteLine("{0}", dev);
                    dev = Math.Sqrt(devsum / Temp[i] - 1);
                    total++;
                    Console.WriteLine(" The Deviation is {0}\n", dev);
                    Console.ReadLine();
                }

            }
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 12-Feb-12 18:22pm    
Did you use debugger to resolve this simple problem?
--SA

1 solution

First, just for reference: "standard deviation" is a second moment of distribution:

http://en.wikipedia.org/wiki/Standard_deviation[^],
http://en.wikipedia.org/wiki/Moment_%28mathematics%29[^],
http://en.wikipedia.org/wiki/Distribution_%28mathematics%29[^].

Let's check up: you correctly populate the distribution Range (hard-coded immediate constants 4, 10 and 50 is really bad thing, I hope you replace it in near future with explicitly declared constants or variable; to your credit, I appreciate that at least you never write any such constants twice), you correctly calculate individual deviations for each value.

From this point, you do it wrong. You need to summarize the individual deviations (squared) in the loop (also use foreach, no need to use for), then you need to take a square root of the sum of squared deviations, after the cycle, but you did it in the cycle. You also should not touch total calculate in the previous loop. You don't need it after avg is calculated. This is a bug.

So, in your last loop you should do this:
C#
double sum = 0;
foreach(double value in Temp) {
    double dev = value - avg;
    sum += dev * dev;
}
float standardDeviation = Math.Sqrt(sum);
Problem solved.

Some side recommendations: 1) never try to do all the work in your entry point (Main), white a separate class and methods; 2) don't use such short variable names like "i", even if this is a loop variable; use something semantic: index, value, counter, element… 3) use the Debugger on any slightest concern about your run-time behavior; at least make sure you do it before asking questions like this one.

[EDIT]

Lan Fan found another bug: you put ReadLine in the loop. Maybe, this is not a mistake, but it could prevent you from seeing what's going on.

Nevertheless, my solution I've written above was correct in first place.
Again, don't use I/O for such a simple debugging, use the Debugger.

—SA
 
Share this answer
 
v9
Comments
LanFanNinja 12-Feb-12 18:41pm    
+ 5
Sergey Alexandrovich Kryukov 12-Feb-12 18:45pm    
Thank you very much.
--SA
LanFanNinja 12-Feb-12 18:48pm    
You're welcome and thank you for pointing that out to me. :)
ericsommerfelt 13-Feb-12 0:08am    
Honestly, I'm not good at math, although I love logic. Even with the Wiki examples it took me forever to get the algorithm I had. I really appreciate the help, Thank You So Much!!!
Sergey Alexandrovich Kryukov 13-Feb-12 0:26am    
Do you call standard deviation a math? :-) To me, this is something about 9-th grade at school (where 1st grade starts with learning alphabet). That's right, no kidding.

You are very welcome.
Good luck, learn some mathematics, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900