Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Calculate the median in Java


(!)

theFAst0ne's Avatar
theFAst0ne theFAst0ne is offline
Computer Specs
Member with 74 posts.
THREAD STARTER
 
Join Date: Apr 2009
Location: Johannesburg, South Africa
Experience: Intermediate
31-Oct-2010, 07:07 AM #1
Calculate the median in Java
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
File Type: zip array.zip (1.1 KB, 56 views)
trevorma91's Avatar
trevorma91 trevorma91 is offline
Computer Specs
Member with 71 posts.
 
Join Date: Dec 2007
Experience: Intermediate
01-Nov-2010, 07:39 PM #2
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.
s_gowtham's Avatar
s_gowtham s_gowtham is offline
Computer Specs
Junior Member with 16 posts.
 
Join Date: Sep 2010
Experience: Advanced
15-Nov-2010, 09:16 AM #3
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[i];
num[i]=num[j+1];
num[j+1]=t;
this sorts it and then the median is num[50]
so the program is
Quote:
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]);
}
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[i];
num[i]=num[j+1];
num[j+1]=t;
}
}
}
med=num[50];

System.out.println ("Highest: " + high);
System.out.println ("Median: " + med);
}
}
check for errors ,there might be mistakes. p.s:-from http://hacktheplanets.blogspot.com/2...selection.html
selection sort
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
15-Nov-2010, 09:58 AM #4
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)
s_gowtham's Avatar
s_gowtham s_gowtham is offline
Computer Specs
Junior Member with 16 posts.
 
Join Date: Sep 2010
Experience: Advanced
16-Nov-2010, 08:36 AM #5
the finer points.....
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


Tags
array, calculate, java, median, statistics

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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


Similar Threads
Title Thread Starter Forum Replies Last Post
On my other laptop it doesnt go to the log in page where I have to type my password dilaksan Windows XP 86 05-Aug-2010 06:29 PM
Calculating the LCM in ASSEMBLY xMikex Software Development 8 27-Apr-2009 09:55 PM
Calculating the number of rows which have a certain criteria cowplopmorris Business Applications 3 28-Sep-2008 08:16 AM
Calculating future dates in a table Chukesgirl Business Applications 1 09-Jun-2008 08:31 AM
Solved: help calculating the number in a report less than zero 109boW Business Applications 2 10-Oct-2007 03:07 PM

WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑