Population Standard Deviation - java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cemma
    New Member
    • Aug 2013
    • 1

    Population Standard Deviation - java

    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

    Code:
            double dayOne, dayTwo, dayThree, dayFour, dayFive, daySix, daySeven, avgRain = 0, stanDeviation = 0, dayOneSq = 0, dayTwoSq, dayThreeSq, dayFourSq, dayFiveSq, daySixSq, daySevenSq, avgRainSq; 
            
            
            //Get rainfall information from users.
            
            System.out.println("Enter rainfall measurement for Day 1 (in mm): ");
            dayOne = reader.nextDouble();
            
            System.out.println("Enter rainfall measurement for Day 2 (in mm): ");
            dayTwo = reader.nextDouble();
    
    Etc, Etc.
    
         //find average
            
    avgRain = (dayOne + dayTwo + dayThree + dayFour + dayFive + daySix + daySeven)/7;
    	
         // calculate population deviation
            
            dayOneSq = (dayOne - avgRain);
            	  Math.pow(2,dayOneSq);
    	
            dayTwoSq = (dayTwo - avgRain);
            	Math.pow(2,dayTwoSq);
            
            etc..etc...
            
            //find standard deviation
            
            avgRainSq = (dayOneSq + dayTwoSq + dayThreeSq + dayFourSq + dayFiveSq
            				+ daySevenSq)/7;
           
            stanDeviation = (Math.sqrt(avgRainSq));
    
    
    System.out.printf (", Standard Deviation: %.4f\n\n",stanDeviation);
    any hint would be fantastic!! Thanks!!!
    Last edited by Rabbit; Aug 5 '13, 03:54 PM. Reason: Please use code tags when posting code.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    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]"

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      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.
      Last edited by Rabbit; Aug 5 '13, 04:02 PM.

      Comment

      Working...