Results 1 to 2 of 2
Thread: Finding the Median
- 02-07-2012, 08:06 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Finding the Median
Java Code:public class JArray { public JArray() { } public JArray(int A) { HowMany=A; } //facilators public void Populate()//put numbers in array { int K; int P; /*for(K=0;K<10;K++)//random numbers { System.out.println("random numbers are "+Math.random()); }*/ K=0; while (K<HowMany) { //i plan to create random numbers between 1-99 P=(int)(Math.random()*100);//casting double into int. ex: (int)(stuff to cast) //numbers 10-99 constrictor if(P>9 && P<100) { Numbers[K]=P; K++; } } } public void ShowThem() { int K; for(K=0;K<HowMany;K++) System.out.println("\tNumber "+(K+1)/*puts 1-20*/+" is "+Numbers[K]); } public void Average()//get average { int K; int Sum; Sum=0; double Average; for(K=0;K<HowMany;K++) { Sum+=Numbers[K];//add all numbers } Average=Sum/HowMany; System.out.println("\tThe Average is "+Average); } public void TheBiggestNumber()//shows biggest number { int K; int Biggest; Biggest=Numbers[0]; for(K=1;K<HowMany;K++) { if(Numbers[K]>Biggest) Biggest=Numbers[K]; } System.out.println("\tThe biggest number is "+ Biggest); } public void TheSmallestNumber()//shows smallest number { int K; int Smallest; Smallest=Numbers[0]; for(K=1;K<HowMany;K++) { if(Numbers[K]<Smallest) Smallest=Numbers[K]; } System.out.println("\tThe smallest number is "+ Smallest); } public void SortTime() { System.out.println("method called"); int K; int Temp; boolean AmIDone; boolean DidISwap; AmIDone=false; while(!AmIDone) { System.out.println("in while loop"); AmIDone=true; DidISwap=false; for(K=0;K<HowMany-1;K++)//the minus 1 is to avoid an index error { if (Numbers[K]>Numbers[K+1]) { Temp=Numbers[K]; Numbers[K]=Numbers[K+1]; Numbers[K+1]=Temp; DidISwap=true; } } if(DidISwap) AmIDone=false; } System.out.println("array sorted"); } public void Reverse() { //System.out.println("method called"); int K; int Temp; boolean AmIDone; boolean DidISwap; AmIDone=false; while(!AmIDone) { //System.out.println("in while loop"); AmIDone=true; DidISwap=false; for(K=0;K<HowMany-1;K++)//the minus 1 is to avoid an index error { if (Numbers[K]<Numbers[K+1]) { Temp=Numbers[K]; Numbers[K]=Numbers[K+1]; Numbers[K+1]=Temp; DidISwap=true; } } if(DidISwap) AmIDone=false; } //System.out.println("array sorted"); } public void Median() { int length=Numbers.length; int[] sort = new int[length]; System.arraycopy(Numbers, 0, sort, 0, sort.length); Arrays.sort(Numbers); if (length % 2 == 0) { System.out.println("\tThe Median is "+(sort[(sort.length / 2) ] + sort[sort.length / 2]) / 2); } else { System.out.println("\tThe Median is "+sort[sort.length / 2]); } } /*int Median; int K; Median=0; for(K=0;K<HowMany;K++) { }*/ private int HowMany; private int Numbers[] = new int[100]; }This code works and runs but when i change it because i only want 20 numbers instead of 100 my median keeps coming out 0 ever time. Why is that?Java Code:public class JTestTwo { public static void main(String[] args) { JArray Me = new JArray(100); Me.Populate(); Me.SortTime(); Me.ShowThem(); Me.TheBiggestNumber(); Me.TheSmallestNumber(); Me.Average(); Me.Median(); Me.Reverse(); Me.ShowThem(); } }
- 02-07-2012, 01:30 PM #2
Re: Finding the Median
Is the posted code the version that is not working?
Please copy and paste here the contents of the console window from when you execute the program showing its output.
You should try debugging the code by adding lots of println statements to show the execution flow and the values of the variables as the program executes. The output should show you what the code is doing so you can correct it.Why is that?
Similar Threads
-
Calculating Median and mode
By rochla16 in forum New To JavaReplies: 4Last Post: 04-29-2011, 04:24 AM -
finding max value
By rocky86 in forum New To JavaReplies: 6Last Post: 10-10-2010, 01:37 AM -
Finding the largest value.
By Blacky777 in forum New To JavaReplies: 7Last Post: 02-02-2010, 08:52 PM -
Finding Median of X Integers
By Hasan in forum New To JavaReplies: 3Last Post: 08-12-2008, 02:06 PM -
Quick sort with median-of-three partitioning
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks