By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

Population Standard Deviation - java

P: 1
Hello!

So i am a new programmer and i really need some help. I am trying to find the Population Standard Deviation of 7 numbers which the user inputs.

Please see below my

Expand|Select|Wrap|Line Numbers
  1.         double dayOne, dayTwo, dayThree, dayFour, dayFive, daySix, daySeven, avgRain = 0, stanDeviation = 0, dayOneSq = 0, dayTwoSq, dayThreeSq, dayFourSq, dayFiveSq, daySixSq, daySevenSq, avgRainSq; 
  2.  
  3.  
  4.         //Get rainfall information from users.
  5.  
  6.         System.out.println("Enter rainfall measurement for Day 1 (in mm): ");
  7.         dayOne = reader.nextDouble();
  8.  
  9.         System.out.println("Enter rainfall measurement for Day 2 (in mm): ");
  10.         dayTwo = reader.nextDouble();
  11.  
  12. Etc, Etc.
  13.  
  14.      //find average
  15.  
  16. avgRain = (dayOne + dayTwo + dayThree + dayFour + dayFive + daySix + daySeven)/7;
  17.  
  18.      // calculate population deviation
  19.  
  20.         dayOneSq = (dayOne - avgRain);
  21.               Math.pow(2,dayOneSq);
  22.  
  23.         dayTwoSq = (dayTwo - avgRain);
  24.             Math.pow(2,dayTwoSq);
  25.  
  26.         etc..etc...
  27.  
  28.         //find standard deviation
  29.  
  30.         avgRainSq = (dayOneSq + dayTwoSq + dayThreeSq + dayFourSq + dayFiveSq
  31.                         + daySevenSq)/7;
  32.  
  33.         stanDeviation = (Math.sqrt(avgRainSq));
  34.  
  35.  
  36. System.out.printf (", Standard Deviation: %.4f\n\n",stanDeviation);
any hint would be fantastic!! Thanks!!!
Aug 5 '13 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 785
Please correct your post and use code tag to format your code.
You forgot to mention your problem. What did not work in a way you expected?

Tip:
You can reduce your code size by using arrays (or Lists) and for-loops: You should not use double day1, day2, day3 etc., but "double[] days = new double[7]" and access for example day 3 by "days[2]" ,or using a loop "for(int i=0; i < 7; i++){...}" with "day[i]"
Aug 5 '13 #2

Rabbit
Expert Mod 10K+
P: 12,421
Please use code tags when posting code.

You never assign the result of math.pow to anything. So you end up never having your squared differences. Also, your arguments need to be reversed, it's the first argument to the power of the second argument, not the reverse.
Aug 5 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.