Results 1 to 2 of 2
  1. #1
    system5634 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default 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];
    
    }
    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();
    	
    		
    		
    	}
    
    }
    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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.
    Why is that?
    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.

Similar Threads

  1. Calculating Median and mode
    By rochla16 in forum New To Java
    Replies: 4
    Last Post: 04-29-2011, 04:24 AM
  2. finding max value
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 01:37 AM
  3. Finding the largest value.
    By Blacky777 in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 08:52 PM
  4. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 02:06 PM
  5. Quick sort with median-of-three partitioning
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •