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;
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 ).
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 ;
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
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.
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
if (x > y && x > z)
cout <<"Max: " << x <<endl;
elseif (y > x && y > z)
cout <<"Max: " << y <<endl;
elseif (z > y && z > x)
cout <<"Max: " << z <<endl;
if((x >= y && x <= z) || (x >= z && x <= y))
cout << "Med: "<< x <<endl;
elseif((y >= x && y <= z) || (y >= z && y <= x))
cout << "Med: "<< y <<endl;
elseif((z >= y && z <= x) || (z >= x && z <= y))
cout << "Med: "<< z <<endl;
if (x < y && x < z)
cout << "Min: " << x << endl;
elseif (y < x && y < z)
cout << "Min: " << y << endl;
elseif (z < y && z < x)
cout << "Min: " << z << endl;
but I couldn't understand your ways it absolutely shorter