472,805 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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.*;
  3. public class ListStats {
  5.     public static Scanner kbd = new Scanner (System.in);
  7.     public static final int MAXSIZE = 4;
  9.     public static void main(String[] args) {
  11.         int[] nums = new int[MAXSIZE];
  12.         int usedSize;
  13.         double median, average;
  14.         String cont;
  16.         System.out.println("Enter a list of integers from smallest to largest (enter negative number to" +
  17.         " end list): ");
  19.         usedSize = getNums(nums);
  20.         median = calcMedian(nums);
  21.         average = calcAverage(nums);
  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);        
  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"));
  38.     }
  39.     public static int getNums(int[]nums){
  41.         int index = 0;
  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.     }
  57.     public static double calcMedian(int[]nums){
  59.         double med;
  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.     }
  69.     public static double calcAverage(int[]nums){
  71.         double avg;
  72.         int sum = 0;
  74.         for ( int x=0; x < nums.length; x++ ) {
  75.             sum = sum + nums[x]; }
  76.         avg = (double)sum / nums.length;
  77.         return avg;
  78.     }
  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. }
Apr 13 '09 #1
1 3470
11,448 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,

Apr 13 '09 #2

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

Similar topics

by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
by: Felix | last post by:
Dear Sql Server experts: First off, I am no sql server expert :) A few months ago I put a database into a production environment. Recently, It was brought to my attention that a particular...
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
by: David Helgason | last post by:
I'm calling one stored procedure with a prepared statement on the server with 6 arrays of around 1200 elements each as parameters. The parameters are around 220K in total. This is taking a...
by: Renjini | last post by:
hi everyone, i have a problem. i have been using an FFT method for 1D arrays. now i need to extend the code for 2D arrays..i thought it would be simple and i managed to implement the FFT method...
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
by: cameron | last post by:
Hi everyone, I am in the process of writing my last program for my Introduction to C++ class. I have to write a program what will allow a end user to input an amount of a mortgage, from there, I...
by: Martien van Wanrooij | last post by:
I am working on some financial calculators and although I succeeded to created the required formulas I am not sure about the following.To give an example: when somebody puts a capital on the bank...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: lllomh | last post by:
How does React native implement an English player?
by: Mushico | last post by:
How to calculate date of retirement from date of birth
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.