467,074 Members | 921 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Arrays -- calculating statistics

My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what the while loop in the getNums method does. However, it is including this negative number when calculating the avg and it is doing something weird to the median. When the array end itself because the max size has been reached everything works fine. Can anyone help me fix this so that when I use type in a negative number it closes the array and then calculates the stats? I am only having a problem with the average and median...smallest number, largest number, and array size are all correct.


Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class ListStats {
  4.  
  5.     public static Scanner kbd = new Scanner (System.in);
  6.  
  7.     public static final int MAXSIZE = 4;
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         int[] nums = new int[MAXSIZE];
  12.         int usedSize;
  13.         double median, average;
  14.         String cont;
  15.  
  16.         System.out.println("Enter a list of integers from smallest to largest (enter negative number to" +
  17.         " end list): ");
  18.  
  19.         usedSize = getNums(nums);
  20.         median = calcMedian(nums);
  21.         average = calcAverage(nums);
  22.  
  23.         System.out.println("Total numbers read: " + (usedSize));
  24.         System.out.println("Smallest number in list: " + nums[0]);
  25.         System.out.println("Largest number in list: " + nums[usedSize-1]);
  26.         System.out.println("The median is: " + median);
  27.         System.out.println("The average is: " + average);        
  28.  
  29.         do{
  30.             System.out.print("\nType a value to search for: ");
  31.             int value = kbd.nextInt();
  32.             int pos = search(nums, usedSize, value);
  33.             System.out.println("The position is: " + pos);
  34.             System.out.print("\nSearch for another integer? (Y or N)");
  35.             cont = kbd.next();
  36.         }while (cont.equalsIgnoreCase("Y"));
  37.  
  38.     }
  39.     public static int getNums(int[]nums){
  40.  
  41.         int index = 0;
  42.  
  43.         int userEntry = kbd.nextInt();
  44.         while(userEntry >= 0 && index+1 < MAXSIZE){
  45.             nums[index] = userEntry;
  46.             index++;
  47.             userEntry = kbd.nextInt();
  48.         }
  49.         if (index+1 == MAXSIZE){
  50.             nums[index] = userEntry;
  51.             index++;
  52.             System.out.println("\nMaximum size has been reached.");
  53.         }
  54.         return index;
  55.     }
  56.  
  57.     public static double calcMedian(int[]nums){
  58.  
  59.         double med;
  60.  
  61.         if (nums.length % 2 != 0)
  62.             med = nums[nums.length/2];
  63.         else {
  64.             med = ((double)nums[(nums.length/2)-1] + (double)nums[nums.length/2]) / 2;
  65.         }
  66.         return med;
  67.     }
  68.  
  69.     public static double calcAverage(int[]nums){
  70.  
  71.         double avg;
  72.         int sum = 0;
  73.  
  74.         for ( int x=0; x < nums.length; x++ ) {
  75.             sum = sum + nums[x]; }
  76.         avg = (double)sum / nums.length;
  77.         return avg;
  78.     }
  79.  
  80.     public static int search(int[]nums, int usedSize, int value){
  81.         for( int pos=0; pos < usedSize; pos++ ) {
  82.             if ( nums[pos] == value ) {
  83.                 return pos;
  84.             }
  85.         }
  86.         return -1;
  87.     }
  88. }
  89.  
  90.  
  91.  
Apr 13 '09 #1
  • viewed: 3241
Share:
1 Reply
Expert 8TB
Your 'usedSize' variable is a local variable, i.e. your other methods know nothing about it; you should pass it to those functions as well (as an another parameter) so those functions can use that instead of the array.length value.

kind regards,

Jos
Apr 13 '09 #2

Post your reply

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

Similar topics

12 posts views Thread by Dave Theese | last post: by
9 posts views Thread by Charles Banas | last post: by
17 posts views Thread by romixnews@googlemail.com | last post: by
reply views Thread by cameron | last post: by
2 posts views Thread by Martien van Wanrooij | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.