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).

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]);
}
}
}
```

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

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

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]);
```

http://www.experts-exchange.com/Programming/Languages/Java/Q_27081221.html?sfQueryTermInfo=1+10+30+median+yan

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;
}
}
}
```

and also, System.out.println("Median

would not be correct as the syntax is wrong!

Also, the variable middle isnt accessible in the median function.

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 ;)

```
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();
}
}
```

Output;

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

```
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;
}
}
}
```

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial