Finding the Median of 5 numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Storm9
    New Member
    • Mar 2007
    • 1

    Finding the Median of 5 numbers

    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.
  • Bl00dFox
    New Member
    • Mar 2007
    • 12

    #2
    I too, am interested in finding the answer... any ideas?

    Comment

    • scruggsy
      New Member
      • Mar 2007
      • 147

      #3
      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.

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by scruggsy
        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

        Code:
        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.

        Comment

        Working...