1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Calculate the median in Java

Discussion in 'Software Development' started by theFAst0ne, Oct 31, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. theFAst0ne

    theFAst0ne Thread Starter

    Joined:
    Apr 16, 2009
    Messages:
    74
    I have written a program in java that puts 100 random integers into an array. I now need to find the median of these numbers. I am still a beginner in java, can anyone help me? Thanks.

    import java.util.*;

    public class array
    {
    public static void main (String [] args)
    {
    int [] num = new int [100];
    int high = 0;
    int med = 0;

    for (int loop = 0 ; loop <=99 ; loop++)
    {
    num [loop] = (int) (Math.random() * 101);

    if (num [loop] > high)
    {
    high = num [loop];
    }
    System.out.println (num [loop]);
    }
    System.out.println ("Highest: " + high);
    System.out.println ("Median: " + med);
    }
    }
     

    Attached Files:

  2. trevorma91

    trevorma91

    Joined:
    Dec 3, 2007
    Messages:
    71
    Ok, so your loop for adding the random numbers is good, along with your determination of the highest number. Why are you doing your random number between 1 and 101? If you're doing numbers 1 - 100 then the median would be 50, correct me if I'm wrong. Then you could just check if num[loop] is equal to 50.
     
  3. s_gowtham

    s_gowtham

    Joined:
    Sep 26, 2010
    Messages:
    16
    trevorma91 you are wrong ,it is the middle number when arranged in ascending or descending order so this is how the program must be

    int t;
    for(int i=0;i<100;i++){
    for(int j=0;j<100-i;j++){
    if(num[j]>num[j+1]){
    t=num;
    num=num[j+1];
    num[j+1]=t;
    this sorts it and then the median is num[50]
    so the program is

    check for errors ,there might be mistakes. p.s:-from http://hacktheplanets.blogspot.com/2010/06/java-program-to-prove-that-selection.html
    selection sort
     
  4. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    If you have an even number of numbers, there is no middle number. You have to divide the set into two halves, then add the last number in the first half plus the first number in the second half and divide by two to get the median of the set.
    1, 7, 12
    7 is the middle number, so it is the median
    1, 7, 12, 23
    No middle number, so:
    (7+12)/2=9.5
    The median is 9.5

    Remember that arrays start with 0, not 1
    If you are checking a list of 100 numbers (Array(0)-Array(99)), the median is the average of the 50th and 51st numbers, or (Array(49)+Array(50))/2

    If you have an odd number of numbers, then it's the middle number.
    If you have 101 numbers (Array(0)-array(100)), the median is the 51st number (101+1)/2, or Array(50)
     
  5. s_gowtham

    s_gowtham

    Joined:
    Sep 26, 2010
    Messages:
    16
    the finer points.....
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/959637