Mkelliny

asked on

# How to locate and display the Median value of an Array

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

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:

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

This is the similar question with accepted solution:

https://www.experts-exchange.com/questions/27081221/The-median-of-a-list-is-its-middle-value-when-the-values-are-placed-in-order-What-I-want-to-to-is-write-an-application-that-allows-you-to-enter-nine-double-values-and-display-them-and-their-median.html?sfQueryTermInfo=1+10+30+median+yan

https://www.experts-exchange.com/questions/27081221/The-median-of-a-list-is-its-middle-value-when-the-values-are-placed-in-order-What-I-want-to-to-is-write-an-application-that-allows-you-to-enter-nine-double-values-and-display-them-and-their-median.html?sfQueryTermInfo=1+10+30+median+yan

ASKER

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

Would this be correct?

System.out.println("Median

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:

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

ASKER

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

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.

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.

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

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

ASKER

Thank you for the post manishkungwani. Just one problem it only seems to be displaying the first 6 values and reapeats them.

ASKER CERTIFIED SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

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.

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

ASKER

sorry the values missing are 754/100.

SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

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

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

Thanks!

Pleasant to read your kind words.

Pleasant to read your kind words.

ASKER

Great advice and duly noted!

Most welcome :)

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