Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How come my code isn\'t giving me the standard deviation??? import java.util.Sca

ID: 3840081 • Letter: H

Question

How come my code isn't giving me the standard deviation???

import java.util.Scanner;

import java.util.*;

public class Program210 {

   public static void main(String[] args) {

      

       double [ ] a = {1, -2, 4, -4, 9, -6, 16, -8, 25, -10};

      

       arraySum(a);

              

   }

   public static void arraySum(double[ ] a){

      

       double alpha = 0;

      

       double beta = 0;

      

       for(int i = 0; i <= a.length-1; i++){

          

           alpha = a[i];

          

           beta = alpha + beta;

          

       }

      

       double average = (beta) / a.length;

      

       double squared = average * average;

      

       double creeper = 0;

      

       for(int y = 0; y <= a.length-1; y++){

          

           double zombie = a[y];

          

           double skeleton = (zombie - squared);

          

           creeper = skeleton + creeper;

          

       }

      

       double numerator = (creeper)/(a.length-1);

      

       double answer = Math.sqrt(numerator);

      

       System.out.println("Your standard deviation is " + answer);

      

   }

  

}

Explanation / Answer

When you debug your code and see value of numerator turns out to be a negative number and then trying to square root of a negative number which is error, we can have square root of negativen numbers.

There is no problem in your code , just the array values being passed.

I just put some Sys out to check values and these are as below :

Inside main Inside arraysum Value of beta after first for loop 25.0 Value of average 2.5 Value of squared 6.25 Value of creeper -37.5 Value of numerator -4.166666666666667 Your standard deviation is NaN

So please provide value which doesn't end up in negative numerator.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote