Coding to find the median of an array

Pages: 12
Hi!
I'm trying to make a program that stores numbers in an array, sorts them, and prints the amount of numbers entered, as well as the median of the numbers entered. I've finished it mostly I think, except for the median part. It's throwing me for a loop a bit. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <iostream>
using namespace std;

int main()
{

	int array[100];
	int temp, index = 0;
	int i=1, j;
	float median;

	while (1)
	{
		cout << "Please enter term #" << i << ": ";
		cin >> temp;
		
		i++;
		
		if (temp < 0) 
			break;
		else 
		{
			array[index++] = temp;
			
			if (index > 100)
			{
				cout << "You have entered too many numbers. Please try again.\n";
				break;
			}
		}
	}

	for (i = 0; i < index; i++)
		for (j = 0; j < index - i - 1; j++)
			if (array[j] < array[j + 1])
			{
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
	/*
	if (index = even)
	{
		median = (array[i/2] + array[i/2]-1)/2;
	}
	else
		median = array[i/2];
        */

	cout << endl << "Numbers now sorted..." << endl;
	for (i = 0; i < index; i++)
		cout << array[i] << " ";
		cout << endl;
		//cout << median
		cout << "The amount of terms entered is " << index << "\n";
	return 0;

}


I put the median code (as far as I know, anyway) in comments. Is that right so far?
I guess the only thing I don't get is how to code the part that makes sure the array is even or odd. How do I write that?

I appreciate the help!
1
2
3
4
5
6
if (index = even)
	{
		median = (array[i/2] + array[i/2]-1)/2;
	}
	else
		median = array[i/2];


If you want to check that the number is odd or event
use mod(%),
1
2
3
4
5
6
if( x%2 == 0 ){ // even
     ...
}else{ // odd
     ...
}
Last edited on
That works!! Thank you so much!
However, it is not printing out the correct median, it won't print decimals. And changing index to a float doesn't work...

An index is always going to be an integer equal to or above 0.

If you have two integers, ex:
1
2
3
int x = 5;
float z;
z = x/2;

z is NOT going to be equal to 2.5. It will be equal to 2 because in C++, it will perform integer division by default.

Do this instead:
1
2
3
int x = 5;
float z;
z = x/2.0f;

This will not truncate the result.


Also, I can't see a reason to declare it like
1
2
int i;
for (i = 0; i < N; i++) {...;}

as opposed to
 
for (int i = 0; i < N; i++){...;}

in C++ (in C you would have to do declare variables beforehand).
In C++, declaring i inside of the for loop scope ensures that you don't accidentally try to access "i" outside of the loop itself.

If your edited code is still in the same section as your original, you are trying to access "i" after it already equals the value of index, so the values being calculated will be array[i/2], which in your case will always be array[50], whatever number that happens to be. Edit: In your case, this might work out to still give the correct answer, but this is bad practice and confusing for someone else to look at.

tl;dr:
So basically, you'd want to change the line to be median = (array[50] + array[49])/2.0f;
Last edited on
1
2
median = (array[i/2] + array[i/2]-1)/2;
median = array[i/2];


Think about it, why 'i'? Which one is the correct for this.


And, why you want to change index to float ?
terapaht: Oops, I meant change i to a float. Sorry about that.

And, messing with my code more, I don't think my formulas are spitting out the right numbers at all... I have no idea why.
do I have the right formulas, or am I even using the right variable??
median = (array[i/2] + array[i/2]-1)/2;
look at this, carefully.

this make you get the wrong POSITION and VALUE.

and for median, you can use y = (float)x; to change x to float and store to y.
i should not have to be a float. The existence of your "index" variable just increases the confusion in your code.
You should set it up more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const int SIZE = 100;
int array[SIZE];

for (int i = 0; i < SIZE; i++)
{
    cout << "Please enter term #" << i << ": ";
    cin >> array[i]; //fixed, I accidently had array[0];
}

//sorting
for (int i = 0; i < SIZE; i++)
{
    for (int j = 0; j < index - i - 1; j++)
    {
        //sorting stuff
    }
}

float median;
if (SIZE%2 == 0) // if SIZE is even (it is for your case)
{
    median = ( array[SIZE/2] + array[SIZE/2 - 1] )/2.0f;
}
else
{
    //ex: if SIZE == 3, array[SIZE/2] will be array[1] due to truncation
    median = array[SIZE/2];

}


Last edited on
Ganado,
The INDEX is alright to store the total number inputted.
1
2
const int SIZE = 100;
int array[SIZE];
is just better for coding in advanced.

BTW you typed it wrong cin >> array[0]; >> cin >> array[i];

And your code won't know how many number inputted. you have to loop to check again, waste of time and code.
Last edited on
Terapaht: Ooooh, that makes complete sense! So array[i/2-1] should give me the correct value instead, right?

Ganado: I think I need the "index" variable to count the amount of times a number is input. Or are you just suggesting that I change the name?
Yes I just noticed that you have a sentinel value if the number entered is below 0, so you would need to keep track of the max index.
So I guess my code wasn't really helpful, but an std::vector would be more convenient to use in this case because you don't know how big of an array you'd need at compile time.
Last edited on
Good job, bro ^^.

It's up to you about variables' name. For me I'll just use 'n', ><.
Another suggestion, you use 'i' instead of 'index' for assigning median, right ?

You just have a luck using it because 'i' just = 'index' in the previous loop.
But if there's some code use 'i' before this, you'll have a non-sense mistake.
Last edited on
Ganado: Yes, I am in a beginners c++ class right now and my professor has not covered that yet, so I'm assuming I'm not supposed to use that.

So I've got my numbers correct, but I still can't print it as a decimal! :(
Post the lines of code where you assign a value to median. Make sure you're dividing the whole thing by /2.0 instead of /2. See my first post for why you need to do this.
 
median = (array[index/2] + array[index/2-1])/2.0f;
Last edited on
OOOOOHHHH, duh! I should have known! I think it's working now, all I had to do was change it to /2.0. Haha I think it works perfectly now!

1
2
3
4
5
6
if (index%2 == 0)
	{
		median = (array[i/2] + array[i/2-1])/2.0;
	}
	else
		median = array[i/2];


Thank you guys so much!!!
read my previous suggestion more, your code works but have a little bit non-sense.
Last edited on
Yes, I wasn't entirely sure what you meant though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (i = 0; i < index; i++)
		for (j = 0; j < index - i - 1; j++)
			if (array[j] < array[j + 1])
			{
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
	if (index%2 == 0)
	{
		median = (array[i/2] + array[i/2-1])/2.0;
	}
	else
		median = array[i/2];


what'll happened if there's
i = 0;
before
1
2
3
4
5
6
if (index%2 == 0)
	{
		median = (array[i/2] + array[i/2-1])/2.0;
	}
	else
		median = array[i/2];


It may seem non-sense about what I'm talking but your 'i' is just like and temporary number and shouldn't be used in median = (array[i/2] + array[i/2-1])/2.0; and median = array[i/2];

You should use your index instead of it.
Last edited on
Hmm. So if I just change all my 'i's to 'index's it should work the same?
Pages: 12