How do I make a method that calculates the variance of each element of an array from the mean of those elements using Java

StackOverflow https://stackoverflow.com/questions/22519990

  •  17-06-2023
  •  | 
  •  

Question

import java.util.Scanner;


public class classGrades
{     
   public static void main(String[] args)
   {

      int DEVIATIONS = 7;
      int SCORES = 7;

      String[] names = {"Bashful" , "Doc" , "Dopey" , "Grumpy" , "Happy" , "Sleepy" , "Sneezy"};
      double[] scores = new double[SCORES];
      double[] variance = new double[DEVIATIONS];
      getScores(scores);
      deviation(scores, variance);


       for(int i =0; i < scores.length; i++)
         {
          System.out.print( names[i] + "\t");  // \t = tab character     
          System.out.print( scores[i] + "\t");  
          System.out.print( variance[i] + "\t");
          System.out.println();
         }  
    meanCalculation(scores);

   }
   public static void getScores(double scores[])
   {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter " + scores.length  + " test scores: ");

      for (int i = 0; i < scores.length ; i++)
         {
            scores[i] = scan.nextDouble();
         }      
   }

   public static void meanCalculation(double[] scores)
   {
      double sum = 0;
      double average = 0;

         for (int index = 0; index < scores.length; index++)
           {    
           sum = sum + scores[index];
            if (scores.length != 0)
                average = sum / scores.length;
             else 
            average = 0;
           } 
       System.out.println("The average grade is " + average);
   }
   public static void deviation(double[] scores, double[] variance)
   {
      double sum = 0;
      double average = 0;
      double sd = 0;
      for (int i = 0; i < scores.length; i++)
           { 
            sum = sum + scores[i];
            average = sum / scores.length;
            variance[i] = scores[i] - average;

            }
   }
 }     

OUTPUT Enter 7 test scores: 1 2 3 4 5 6 7

Bashful 1.0 0.8571428571428572
Doc 2.0 1.5714285714285714
Dopey 3.0 2.142857142857143

Grumpy 4.0 2.571428571428571

Happy 5.0 2.857142857142857

Sleepy 6.0 3.0

Sneezy 7.0 3.0

The average grade is 4.0

The third number in the parallel array is the one I am having trouble with. I successfully computed the average in the meanCalculation method and passed it back to the main method, however, in my deviation method I cannot seem to figure out how to pass each element through the method and subtract the mean from it, to get my variance from the mean. This is due tonight so any help would be greatly appreciated. This is my first time posting please let me know how to better clear up my question. I am using the program JGrasp and am in the language of Java! Thanks again!

Was it helpful?

Solution

Your algorithm is simply not correct. First you need to compute the average and then other number. The statistical variance is the sum of the squares of the deltas of your scores from the average (and would give you one value for the complete series). Your variance seems to be the difference between each score and the average. Does that work:

public static void deviation(double[] scores, double[] variance)
{
    double sum = 0;
    for (int i = 0; i < scores.length; i++){ 
        sum = sum + scores[i];
    }
    double average = sum / scores.length;

    for (int i = 0; i < scores.length; i++){ 
        variance[i] = scores[i] - average;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top