median number

I am trying to sort three entries to max, min and median

I figured max and min but I have problem with median

also I am not sure if my if statements are not proficient and they could be written in better way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 if (x > y && x > z)
		cout <<"Max: : " << x <<endl;
	if (y > x && y > z)
		cout <<"Max: : " << y <<endl;
	if (z > y && z > x)
		cout <<"Max: : " << z <<endl;

	if (x < y && x < z)
		cout << "Min: " << x << endl;
	if (y < x && y < z)
		cout << "Min: " << y << endl;
	if (z < y && z < x)
		cout << "Min: " << z << endl;

	if( x<=y||x<=z && x>=y || x >=z)
		cout << "Median: " << x <<endl;
Last edited on
The median is the middle number. First, You must sort the list of numbers. Second, you must decide if there are an odd or even amount of numbers. If there is an odd amount of numbers the median is simply the middle number (numbers/2). If there is an odd number of numbers the median is the mean of the two middle elements( (numbers/2 + numbers/2 +1)/2 ).
1
2
3
4
5
6
7
8
9
10
int min = x ;
if( y < min ) min = y ;
if( z < min ) min = z ;

int max = x ;
if( y > max ) max = y ;
if( z > max ) max = z ;

// assumes that there won't be an integer overflow
int median = x + y + z - min - max ;
I am sorry but both of two replies didn't help

this is the question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Prompt the user, input 3 double numbers, complain and die in the face of input error.
Output the minimum number, the median number, and the maximum number, with explanatory text.

As you may already know, the median number is the input number that is 

less than or equal to at least 2 of the input numbers as well as greater than

 or equal to at least 2 of the input numbers. (In other words, it’s the number 

left over after you remove the min and the max.)

Don’t assume that the numbers are necessarily in order.
Don’t assume that all the numbers are necessarily distinct.
Don’t assume that all the numbers are necessarily positive.
An example run of your program (with user input in bold) might go as
	3 #’s:  3  2  seven
	Fatal error: input failure
Another example run of your program might go as
	3#’s:2.2  4.4  1.1
	min: 1.1
	median: 2.2
	max: 4.4
Last edited on
both of two replies didn't help
*edit fixed quote tags.

So how exactly do they not help? I Showed one method. Put them in order x y z then min would be x, median would be y and max would be z. JLBorges showed the way your assignment says. median = sum - min - max.
Last edited on
but this won't match the way I did it
I don't see why you would want to make a really long if statement instead of simply assigning to a min/max/median variable then outputting. You do several unnecessary if checks anyways. You should consider using else/if.
As far as doing it your way you should read up on || and && operators a little more. Here is what median would look like if x was the median. if((x >= y && x <= z) || (x >= z && x <= y)) //x is median
you are right I just realized that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	if (x > y && x > z)
		cout <<"Max: " << x <<endl;
	else if (y > x && y > z)
		cout <<"Max: " << y <<endl;
	else if (z > y && z > x)
		cout <<"Max: " << z <<endl;

    if((x >= y && x <= z) || (x >= z && x <= y))
		cout << "Med: "<< x <<endl;
	else if((y >= x && y <= z) || (y >= z && y <= x))
		cout << "Med: "<< y <<endl;
	else if((z >= y && z <= x) || (z >= x && z <= y))
		cout << "Med: "<< z <<endl;
	
     if (x < y && x < z)
		cout << "Min: " << x << endl;
	else if (y < x && y < z)
		cout << "Min: " << y << endl;
	else if (z < y && z < x)
		cout << "Min: " << z << endl;


but I couldn't understand your ways it absolutely shorter
Last edited on
Topic archived. No new replies allowed.