-1

This should be a fairly simple homework assignment, but I've been pounding my face on it for a while now ... When executed, it should just populate an array and find the mean and standard deviation. I'm getting an out of bounds exception, but only in the arrayDeviation method. Any direction would be appreciated.

import java.util.Scanner;
import java.util.Random;

public class StandardDeviation
{
  //declare global variables
final static int ELEMENTS = 100;

public static void main(String [] args)
{
     //declare variables   
  final int RANGE = 500;
  int[] numList = new int[ELEMENTS];

  Random rand = new Random();

     //populate array
  for(int count = 0; count < ELEMENTS; count++)
  {
     numList[count] = rand.nextInt(RANGE) + 1;
  }

     //call printArray
  printArray(numList);

     //call arrayAverage
  double mean = arrayAverage(numList);
  System.out.println("\nMean: " + mean);

     //call arrayDeviation
  double standardDeviation = arrayDeviation(numList, mean);
  System.out.print("Standard deviation: " + standardDeviation);



} //end main



  //output 10 elements per line
public static void printArray(int[] list)
{
  final int ELEMENTS_PER_LINE = 10;

  for(int count = 0; count < ELEMENTS; count++)
  {
     System.out.printf("%-4d", list[count]);
     if ((count + 1) % ELEMENTS_PER_LINE == 0)
     {
        System.out.println();
     }
  } 
}


  //returns average as double
public static double arrayAverage(int[] list)
{
  int sum = 0, count;
  double average;

  for(count = 0; count < ELEMENTS; count++)
  {
     sum = sum + list[count];
  }
  average =(double) sum / count;

  return average;
}

  //calculate and return standard deviation
public static double arrayDeviation(int[] list, double mean)
{
  double sum = 0.0, standardDeviation;
  int count;

  for(count = 0; count < ELEMENTS; count++);
  {
     sum = sum + Math.pow((list[count] - mean), 2);
  }

  standardDeviation = Math.sqrt(sum / 2);

  return standardDeviation;
}

} //end class
mhawkins
  • 1
  • 1

1 Answers1

0

Staring at this code for like 10 minutes I couldn't see why it's giving that Exception.Pasting it in Netbeans and it instatly highlights an empty for-loop

public static double arrayDeviation(int[] list, double mean)
{
  double sum = 0.0, standardDeviation;
  int count;

  for(count = 0; count < ELEMENTS; count++); //This semicolon is your 
                                             //problem
  {
     sum = sum + Math.pow((list[count] - mean), 2);
  }
vlatkozelka
  • 909
  • 1
  • 12
  • 27
  • This is compile error semicolon doesnt give ArrayOutOfIndex exception – Luminous_Dev Nov 27 '16 at 19:51
  • yes but it's causing it, because `count` was still undefined, so `list[count]` gives you the exception since the `for` statement was skipped – vlatkozelka Nov 27 '16 at 19:52
  • @Luminous_Dev I'm guessing you down-voted? I verified the answer in Netbeans and it works giving mean and standard deviation, but I get a down-vote ? Thanks ! – vlatkozelka Nov 27 '16 at 22:07