How to locate and display the Median value of an Array

Mkelliny
Mkelliny used Ask the Experts™
on
I need to create a program that has 9 values in an array they must be sorted in ascending order then displayed. The program is to then find the median value and display it as well. I think that I almost have it, but I am getting a lot of errors.  Any help is extremely appreciated.
import java.util.Collections;
import java.util.Arrays;
public class Median
 {

	public static void main(String[] args)

	{
	
	     	int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
			int numSort = arrayNum.length - 1;
			for(a = 0; a < arrayNum.length - 1; ++a)
			{
				for(b = 0; b < numSort; ++b)
				{
					if(arrayNum[b] > arrayNum[b + 1])
					{
						temp = arrayNum[b];
						arrayNum[b] = arrayNum[b + 1];
						arrayNum[b + 1] = temp;
						System.out.println("Numbers in the array = " + arrayNum[b]);
					}
				}
			
			--numSort;
			}
	}
			
	public static double median(double[] m)
	{
    int middle = m.length/2;  
    if (m.length%2 == 1) {
        
        return m[middle];
    } else 
	 {
       
       return (m[middle-1] + m[middle]) / 2.0;
		 System.out.println("Median number in the array = " + middle[b]);
    }
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
You need to declare variables:

for(a = 0; a < arrayNum.length - 1; ++a)

should be

for(int a = 0; a < arrayNum.length - 1; ++a)


the same thing about variables  b and temp

Awarded 2011
Awarded 2011

Commented:
Even after you declare
int b  in the  for() loop and then int temp
you still have abigger problem because you want to use varaible b in the place where it is out of scope
here:
 System.out.println("Median number in the array = " + middle[b]);

Open in new window

Awarded 2011
Awarded 2011

Commented:
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
I changed the a to Int a,  b into int b, and temp into int temp.  

Would this be correct?
System.out.println("Median number in the array = " + [m]middle);
Awarded 2011
Awarded 2011

Commented:

Post your new code - I'll check .
Ok, so the problems were:
1. you didnt declare, a, b and temp.
2. in median you were using wrong variables.
3. In median function, you were returning before System.out call
4. you did not call median function

The corrected code works perfectly.
Here is the corrected code:

import java.util.Collections;
import java.util.Arrays;
public class Median
 {
	public static void main(String[] args)
	{
	     	int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
			int numSort = arrayNum.length - 1;
			int temp = 0;
			for(int a = 0; a < arrayNum.length - 1; ++a)
			{
				for(int b = 0; b < numSort; ++b)
				{
					if(arrayNum[b] > arrayNum[b + 1])
					{
						temp = arrayNum[b];
						arrayNum[b] = arrayNum[b + 1];
						arrayNum[b + 1] = temp;
						System.out.println("Numbers in the array = " + arrayNum[b]);
					}
				}
			--numSort;
			}
		System.out.println("Median number in the array = " + median(arrayNum));
	}
			
	public static double median(int[] m)
	{
		int middle = m.length/2;  
		if (m.length%2 == 1) {
			return m[middle];
		}
		else{
			return (m[middle-1] + m[middle]) / 2.0;
		}
	}
}

Open in new window

Author

Commented:
Here is the new code, Thanks for that link btw. I was actually thinking about using the array list when I was first writing it, but I didn't know if it would work right.
import java.util.Collections;
import java.util.Arrays;
public class Median
 {

	public static void main(String[] args)

	{
	
	     	int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
			int numSort = arrayNum.length - 1;
			for(int a = 0; a < arrayNum.length - 1; ++a)
			{
				for(int b = 0; b < numSort; ++b)
				{
					if(arrayNum[b] > arrayNum[b + 1])
					{
						int temp = arrayNum[b];
						arrayNum[b] = arrayNum[b + 1];
						arrayNum[b + 1] = temp;
						System.out.println("Numbers in the array = " + arrayNum[b]);
					}
				}
			
			--numSort;
			}
	}
			
	public static double median(double[] m)
	{
    int middle = m.length/2;  
    if (m.length%2 == 1) {
        
        return m[middle];
    } else 
	 {
       
       return (m[middle-1] + m[middle]) / 2.0;
		 System.out.println("Median number in the array = " + [m]middle);
    }
	}
}

Open in new window

As you see, you did not call the median function:

and also,  System.out.println("Median number in the array = " + [m]middle);
would not be correct as the syntax is wrong!
Also, the variable middle isnt accessible in the median function.
oops, i misunderstood :P
m[middle] will work :)
But as in the else, you want the sum, you will have to save it in another variable.

Try and run the code i submitted, it works ;)

Author

Commented:
Thank you for the post manishkungwani. Just one problem it only seems to be displaying the first 6 values and reapeats them.
Awarded 2011
Awarded 2011
Commented:
This is with the ArrayList with your numbers


import java.util.ArrayList;
import java.util.Collections;

public class Median1 {

                   static int [] myArray = { 600, 59, 754, 169, 420, 699, 1000, 800, 999};
    public Median1(){

        ArrayList<Integer> ar = new ArrayList<Integer>();

        for (int i = 0; i < myArray.length; i++) {
            int i1 = myArray[i];
            ar.add(new Integer(i1));
        }

        Collections.sort(ar);

        int pos = (ar.size()-1)/2;

        int num = (ar.get(pos)).intValue();

        System.out.println(ar);
        System.out.println("pos " + pos + " num " + num);


    }

    public static void main(String[] args) {
        new Median1();
    }

}

Open in new window


Output;
[59, 169, 420, 600, 699, 754, 800, 999, 1000]
pos 4 num 699

Open in new window

could you post a screenshot ..
because i can see the correct values ... :/

 Screenshot of correct output.

Author

Commented:
It is in your Screenshot I have the values listed as [59, 169, 420, 600, 699, 754, 800, 999, 1000]

it is displaying as 59, 169, 420, 699, 800, 999, 169, 420. It is missing the values 800 and 1000.

Author

Commented:
sorry the values missing are 754/100.
That was because the printing was done in the inner most loop with the comparison, instead, the printing should be done later: (check the following code)

import java.util.Collections;
import java.util.Arrays;
public class Median
 {
	public static void main(String[] args)
	{
		int[] arrayNum = new int[] {600, 59, 754, 169, 420, 699, 1000, 800, 999};
		int numSort = arrayNum.length-1;
		int temp = 0;
		for(int a = 0; a < arrayNum.length - 1; ++a)
		{
			for(int b = 0; b < numSort; ++b)
			{
				if(arrayNum[b] > arrayNum[b + 1])
				{
					temp = arrayNum[b];
					arrayNum[b] = arrayNum[b + 1];
					arrayNum[b + 1] = temp;
					
				}
			}
		--numSort;
		}
		for(int c = 0; c < arrayNum.length; ++c)
		{
			System.out.println("Numbers in the array = " + arrayNum[c]);
		}			
		System.out.println("Median number in the array = " + median(arrayNum));
	}
			
	public static double median(int[] m)
	{
		int middle = m.length/2;  
		if (m.length%2 == 1) {
			return m[middle];
		}
		else{
			return (m[middle-1] + m[middle]) / 2.0;
		}
	}
}

Open in new window

Author

Commented:
You both are amazing! Thank you so much!  I see my errors and I have learned a great deal from my mistakes and your guys help! :) For yan I especially thank you for all your help in all of my other previous questions you are truly an asset to this community and both of you are extremely appreciated.
Thanx :)

Some advice, for practical purposes, its recommended that you use the available functions in Java for sorting, those are much more efficient and useful..

Arrays are for classrooms ;)
Awarded 2011
Awarded 2011

Commented:
Thanks!
Pleasant to read your kind words.

Author

Commented:
Great advice and duly noted!
Most welcome :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial