Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 12th, 2009, 5:06 PM   #1
poopysammich
Newbie
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0 poopysammich is on a distinguished road
a simple program to find median of x numbers

hi all, my instructor has requested that we create a program to find the median of a variable amount of numbers. the stipulations are that it only be accurate with an odd number of integers and with no repeating numbers. the source code i have devised from the pseudocode we were given is a such
Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class ArrayTest{
  4. public static void main(String args[]){
  5. Scanner input=new Scanner(System.in);
  6.  
  7.  
  8. System.out.print("Enter the number of integers: ");
  9. int size=input.nextInt();
  10. int[] arr=new int[size];
  11.  
  12. System.out.print(arr.length+"indexes\n");
  13.  
  14. for(int i=0;i<size;i++){
  15. System.out.print("enter an integer: ");
  16. int number=input.nextInt();
  17. arr[i]=number;
  18. }
  19.  
  20. int i;
  21. for( i=0;i<size;++i){//for i<size
  22. int count=0;
  23. for (int j=0;j<=size;++j){
  24. if(arr[i]>arr[j]){
  25. count++;
  26.  
  27. }
  28. }
  29. if(count==arr.length/2){
  30. break;
  31.  
  32. }
  33. System.out.print(arr[i]);
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. }
  43. }

i am new to arrays, and missed the lecture, and my instructor is out of town, the program compiles but upon processing for the median of x numbers the error message
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at ArrayTest.main(ArrayTest.java:24)
Press any key to continue...
appears. i am not necessarily asking for a solution, but merely a nudge in the right direction. any thoughts?

Last edited by Sane; Mar 12th, 2009 at 5:12 PM. Reason: Added "=JAVA" for line numbers
poopysammich is offline   Reply With Quote
Old Mar 12th, 2009, 5:15 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: a simple program to find median of x numbers

Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at ArrayTest.main(ArrayTest.java:24)
Press any key to continue...
Translated:

Quote:
Array Index Out Of Bounds at Line 24
Line 24: arr[i]>arr[j]

Out of bounds means you are accessing an element that doesn't exist within the array's bounds.
e.g. Allocating an array of size N and accessing the N'th index (since the first index is 0, and the last index is N-1).
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Mar 12th, 2009, 9:36 PM   #3
poopysammich
Newbie
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0 poopysammich is on a distinguished road
Re: a simple program to find median of x numbers

Quote:
Originally Posted by Sane View Post
Translated:



Line 24: arr[i]>arr[j]

Out of bounds means you are accessing an element that doesn't exist within the array's bounds.
e.g. Allocating an array of size N and accessing the N'th index (since the first index is 0, and the last index is N-1).
thank you. i have corrected the problem. it was the <= j in the header of the for loop.
poopysammich is offline   Reply With Quote
Old Mar 12th, 2009, 9:57 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: a simple program to find median of x numbers

No problem.

If you are interested, as an extra-curricular exercise try to make your solution faster. You will discover that if you make your array contain over 50,000 numbers (you could generate such an array randomly with a for loop) it will take around 5-20 seconds to run. You can get this down under 1 second by first sorting the list efficiently, and then 'doing the obvious'. A similar question could easily be on one of your future assignments or exams, so trying this out is not a waste of time (unless you're just trying to pass, in which case that's a shame).
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Mar 12th, 2009, 10:03 PM   #5
poopysammich
Newbie
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0 poopysammich is on a distinguished road
Re: a simple program to find median of x numbers

Quote:
Originally Posted by Sane View Post
No problem.

If you are interested, as an extra-curricular exercise try to make your solution faster. You will discover that if you make your array contain over 50,000 numbers (you could generate such an array randomly with a for loop) it will take around 3-10 seconds to run. You can get this down under 1 second by first sorting the list efficiently, and then 'doing the obvious'. A similar question could easily be on one of your future assignments or exams, so trying this out is not a waste of time (unless you're just trying to pass, in which case that's a shame).
i am certainly not interested in just passing. the more i work with java the more i enjoy it. as such i am currently working on tweaks for finding accurate median with an even number of inputs and also how to work around duplicate entries. i will definitely have a look at the above algorithm.
poopysammich is offline   Reply With Quote
Old Mar 12th, 2009, 10:09 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Google / Kitchener
Posts: 4,154
Rep Power: 15 Sane has a spectacular aura aboutSane has a spectacular aura about
Send a message via MSN to Sane
Re: a simple program to find median of x numbers

The sorting method will in fact take care of the even amounts and duplicates (can you see why?). On a related note, there are even more efficient solutions which can work for lists of millions of numbers in under one second, but I'm getting too ahead of myself.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges
Rules | How to Post a Question | How to Post Code

Becoming a good programmer requires foresight of your code's execution.
Becoming an excellent programmer requires foresight of your code's modification.
Sane is offline   Reply With Quote
Old Mar 12th, 2009, 10:16 PM   #7
poopysammich
Newbie
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0 poopysammich is on a distinguished road
Re: a simple program to find median of x numbers

Quote:
Originally Posted by Sane View Post
The sorting method will in fact take care of the even amounts and duplicates (can you see why?). On a related note, there are even more efficient solutions (which can process lists of millions of numbers in under one second), but I'm getting too ahead of myself.
lol and possibly a bit too ahead of me, i'm afraid. but yes it does make sense that sorting method would be a means to the end i am chasing.
poopysammich is offline   Reply With Quote
Old Feb 13th, 2012, 4:43 AM   #8
extro
extro
 
Join Date: Jan 2012
Location: Ph
Posts: 49
Rep Power: 0 extro is on a distinguished road
Re: a simple program to find median of x numbers

Ur line 24 has array created like this for (int j=0;j<=size;++j){ making j to be 0 to size(eg. if size is 20, then it will mean that j is 0 to 20 making the total values to be 21 instead of 20. (note: j<=size means 0 to size)
extro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Positive and Negative numbers - won't compile "if" or "else" statements 3dserge C++ 13 Feb 26th, 2009 11:41 AM
find out the average of odd numbers -do-while statement. LAYAN-2008 Java 4 May 6th, 2008 3:24 PM
Simple program with IE window cygnusx Visual Basic 2 Feb 4th, 2006 3:29 AM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 1:15 PM
Problem with using DLL from simple c++ program. scicatur C++ 5 May 12th, 2005 12:46 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC