454,483 Members | 1,612 Online 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
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

 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 sort( a, a+5 );   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 