Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-11-2009
Member
 
Join Date: May 2009
Posts: 999
how to find median of given number in java?

Hi,
I am new to programming language. I recently started learning java language. Yesterday our sir has given us program like find median of given numbers in java?. I use various method but I unable to write that program. That's why I asked this question on this forum. So if you have any idea about this please help me.
Reply With Quote
  #2  
Old 26-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: how to find median of given number in java?

Here I posted code for finding median, try to compile in your computer.


Code:
   
      import java.util.Scanner;
  
      public class arraytesting{
   
      public static void main(String args[])
     {
  
      Scanner p=new Scanner(System.in);
 
      System.out.print("Enter the numbers : ");

      int s=p.nextInt();

      int[] arr1=new int[s];

      System.out.print(arr1.length+"indexes\n");
 
      for(int p=0;p<size;p++){

      System.out.print("enter an integer number: ");
 
      int num=p.nextInt();

      arr[p]=num;

      }

      int p;

      for( p=0;p<size;++p){//for p<s

      int count=0;

      for (int t=0;t<=s;++t){

      if(arr1[p]>arr1[p]){

      count++;
  
      }
 
      }

      if(count==arr1.length/2){

      break;
 
  
      }

      System.out.print(arr1[p]);

      }
      }
      }
Reply With Quote
  #3  
Old 26-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: how to find median of given number in java?

Here is simple code to find median. Try to understand each line.


Code:
package net.sourceforge.jiu.util;


public class MedianNum
{
	
	private MedianNum()
	{
	}

	
	public static void swaping(int[] p, int s1, int s2)
	{
		int temp1 = p[si1];
		p[s1] = p[s2];
		p[s2] = temp1;
	}

	
	public static int find(int[] p, int from, int to)
	{
		int lowvalue = from;
		int highvalu = to;
		int med = (lowvalu + highvalu) / 2;
		do
		{
			if (highvalu <= lowvalu)
			{
				return a[med];
			}
			if (highvalu == lowvalu + 1)
			{
				if (a[lowvalu] > a[highvalu])
				{
					swaping(p, lowvalu, highvalu);
				}
				return p[med];
			}
			int middle = (lowvalu + highvalu) / 2;
			if (p[middle] > p[highvalu])
			{
				swaping(a, middle, highvalu);
			}
			if (p[lowvalu] > p[highvalu])
			{
				swaping(p, lowvalu, highvalu);
			}
			if (p[middle] > p[lowvalu])
			{
				swaping(p, middle, lowvalu);
			}
			swaping(p, middle, lowvalu + 1);
			int lowl = lowvalu + 1;
			int highh = highvalu;
			do
			{
				do
				{
					lowl++;
				}
				while(a[lowvalu] > p[lowl]);
				do
				{
					highh--;
				}
				while(a[highh] > p[lowvalu]);
				if (highh < lowl)
				{
					break;
				}
				swaping(p, lowl, highh);
			}
			while(true);
			swaping(p, lowvalu, highh);
			if (highh <= median)
			{
				lowvalu = lowl;
			}
			if (highh >= med)
			{
				highvalu = highh - 1;
			}
		}
		while(true);
	}
}
Reply With Quote
  #4  
Old 26-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: how to find median of given number in java?

Here I am not writing any code to find median of given numbers rather I just given you logic behind it. Let's see how we have to find median of given number.If there are 7 numbers then the median will be at number 4 and if there are 8 numbers then also median will be at number 4.

Use following formula to get logic behind it:
MedianPosition = (nuberofitems+1) / 2

It means that if numberofitems is 5 then median will be 3.
I think this will help you.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to find median of given number in java?"
Thread Thread Starter Forum Replies Last Post
Java : test a number to find out if its is even or odd teenQ Software Development 4 06-04-2012 06:42 AM
Determine a whole number in java Remedy Software Development 5 25-02-2010 04:39 AM
How to format a number in java? kamina23 Software Development 4 20-01-2010 09:01 PM
Program : How to calculate the median in C++? UseME Software Development 3 19-01-2010 12:08 PM
How to write java program to find factorial of number? Balamohan Software Development 5 28-11-2009 10:14 PM


All times are GMT +5.5. The time now is 06:58 AM.