Hello, I had ran into a very difficult problem in my CS110 class in University. The problem asks us to use if statements to take 5 random inputted float numbers: ie. 5.63, 10.5, 16.7, 20.6 & 10.9 and take the median value, Ie the value that is in the middle. In this case it would be 10.9. I had thought of taking each inputted number and using > statements to find out which is smallest/largest and thought this would become very time consuming, because, obviously the numbers are inputted in no specific order.

Perhaps using a flag and find the lowest and highest value of the 5, and then taking the middle between those three would somehow work. Its the last question of the chapter, and there is nothing in the notes that discusses how to figure it out. Thus the class is called Programming and Problem Solving for Engineers

you could sort the numbers then its easy to find the middle one. Or add them all up, divide by 5 then check each of the numbers to find the one that is closest to that.

5.63 + 10.5 + 16.7 + 20.6+ 10.9 = 64.33
64.33 / 5 = 12.866
Now, just like the bids on The Price Is Right, the number closest to 12.866 without going over is the winner.

not really. try 1.0, 2.0, 3.0, 4.0, 10.0
what is required is the number with the ordinal rank of 3.

using if as the *only* (control) statement? here's how you could do it for three integer numbers.

``````#include <iostream>
#include <algorithm>

int main()
{
int lowest, /*low,*/ middle, /*high,*/ highest ;
std::cin >> lowest >> middle ;
if( middle < lowest ) std::swap( middle, lowest ) ;
std::cin >> highest ;
if( highest < middle )
{
std::swap( middle, highest ) ;
if( middle < lowest ) std::swap( middle, lowest ) ;
}
std::cout << middle << '\n' ;
}``````

Averaging will not help. Sorting and selecting the middle value will.

since you do know that only 5 random numbers will be the input

try using arrays after declaring float num1,num2...num5

then have something like

``````float numbers[5];
float temp1;
float temp2;
int x;
int y;

numbers[0] = num1;
numbers[1] = num2;
numbers[2] = num3;
numbers[3] = num4;
numbers[4] = num5;

for(x = 0; x < 4; x++)
{
for(y = x+1; y < 5; y++)
{
if(numbers[x] <= numbers[y])
{
temp1 = numbers[y];//here
temp2 = numbers[x];//happens
numbers[x] = temp1;//the
numbers[y] = temp2;//swap
}
}
}

cout<<numbers[2];//of 0-4, 2 is the middle.``````

basically it's almost the same as what vijayan21 posted,only i didn't use algorithm

not really. try 1.0, 2.0, 3.0, 4.0, 10.0
what is required is the number with the ordinal rank of 3.

1 + 2 + 3 + 4 + 10 = 20
20 / 5 = 4

I think you are right :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.