By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

Finding the Median of 5 numbers

P: 1
I have to:
"Use functional decomposition to write a C++ program that determines the median of five input numbers. The median is the middle number when the five are arranged in order. However, the user can input the values in any order, so your program must determine which value is between the other two. For example, if the user enters:

41.52 27.18 96.03 12.5 13.8

then the program would output:

The median of 41.52, 27.18, 96.03, 12.5, and 13.8 is 41.52."

Any Ideas where to start? I am so confused, on how to get a program to find the median.
Mar 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: 12
I too, am interested in finding the answer... any ideas?
Mar 18 '07 #2

100+
P: 147
One way to go about it would be to first sort the numbers from lowest to highest. Then, since you know there are exactly five numbers, the third must be the median.
Your problem then becomes sorting the numbers; finding the median is simple after that's done.
Sorting is a very common task. Try a forum search for some pointers.
Mar 18 '07 #3

arne
Expert 100+
P: 315
One way to go about it would be to first sort the numbers from lowest to highest. Then, since you know there are exactly five numbers, the third must be the median.
Your problem then becomes sorting the numbers; finding the median is simple after that's done.
Sorting is a very common task. Try a forum search for some pointers.
Sorting is even so common that the STL contains a function called sort, so you should be able to simply type

Expand|Select|Wrap|Line Numbers
  1. sort( a, a+5 );
  2.  
to sort your array named a with the 5 numbers. Unless you really want to do the sorting by yourself (which is a good exercise!), I would always prefer the STL algorithms.
Mar 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.