Question
I have 5 values and am trying to find the median without using arrays. If i plugged 1 2 3 4 5 my meadian is
I have 5 values and am trying to find the median without using arrays. If i plugged 1 2 3 4 5 my meadian is coming out as 1 what is worng? C++
if ((val1 <= val2 && val1 <= val3 && val1 >= val4 && val1 >= val5) ||
(val1 <= val2 && val1 <= val4 && val1 >= val3 && val1 >= val5) ||
(val1 <= val2 && val1 <= val5 && val1 >= val3 && val1 >= val4) ||
(val1 <= val3 && val1 <= val4 && val1 >= val2 && val1 >= val5) ||
(val1 <= val3 && val1 <= val5 && val1 >= val2 && val1 >= val4) ||
(val1 <= val4 && val1 <= val5 && val1 <= val2 && val1 <= val3)) {
medianVal = val1;
}
else if ((val2 <= val1 && val2 <= val3 && val2 >= val4 && val2 >= val5) ||
(val2 <= val1 && val2 <= val4 && val2 >= val3 && val2 >= val5) ||
(val2 <= val1 && val2 <= val5 && val2 >= val3 && val2 >= val4) ||
(val2 <= val3 && val2 <= val4 && val2 >= val1 && val2 >= val5) ||
(val2 <= val3 && val2 <= val5 && val2 >= val1 && val2 >= val4) ||
(val2 <= val4 && val2 <= val5 && val2 <= val1 && val2 <= val3)) {
medianVal = val2;
}
else if ((val3 <= val1 && val3 <= val2 && val3 >= val4 && val3 >= val5) ||
(val3 <= val1 && val3 <= val4 && val3 >= val2 && val3 >= val5) ||
(val3 <= val1 && val3 <= val5 && val3 >= val2 && val3 >= val4) ||
(val3 <= val2 && val3 <= val4 && val3 >= val1 && val3 >= val5) ||
(val3 <= val2 && val3 <= val5 && val3 >= val1 && val3 >= val4) ||
(val3 <= val4 && val3 <= val5 && val3 <= val1 && val3 <= val2)) {
medianVal = val3;
}
else if ((val4 <= val1 && val4 <= val2 && val4 >= val3 && val4 >= val5) ||
(val4 <= val1 && val4 <= val3 && val4 >= val2 && val4 >= val5) ||
(val4 <= val1 && val4 <= val5 && val4 >= val2 && val4 >= val3) ||
(val4 <= val2 && val4 <= val3 && val4 >= val1 && val4 >= val5) ||
(val4 <= val2 && val4 <= val5 && val4 >= val1 && val4 >= val3) ||
(val4 <= val3 && val4 <= val5 && val4 <= val1 && val4 <= val2)) {
medianVal = val4;
} else {
medianVal = val5;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started