# Find the Median

Page 1 of 1

## 4 Replies - 2983 Views - Last Post: 03 October 2012 - 01:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294032&amp;s=c3b0114de4c76bd5902d067db0ed5a39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 byron2012

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 02-October 12

# Find the Median

Posted 02 October 2012 - 08:10 PM

I am using a do-while statement for a project. we are to receive 5 names, prices, and discount factors for an 'online store' and then create a menu with options like find the median price, find the average price, find the lowest price.

My question is how do I go about writing the function to call for the median? obviously, I will need to arrange the prices in ascending order and go from there-and this is where I am stuck. I am not sure how to arrange the prices in ascending numerical order.

Is This A Good Question/Topic? 0

## Replies To: Find the Median

### #2 Skydiver

• Code herder

Reputation: 6155
• Posts: 21,228
• Joined: 05-May 12

## Re: Find the Median

Posted 02 October 2012 - 08:33 PM

You could compute the prices and store them in array. Once you have the array of prices, sort the array. Once you have the sorted array, determine if you have an odd or even number of items. In this specific case, you have an odd number because you have 5 items. So go to the 3rd item in the array and that will be your median.
Was This Post Helpful? 0

### #3 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Find the Median

Posted 02 October 2012 - 10:31 PM

C or C++ has qsort.

C++ has the sort algorithm - sort.

If you want to write your own sort function, there is a bubble sort or a selection sort for example.
Was This Post Helpful? 0

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Find the Median

Posted 02 October 2012 - 10:49 PM

Since your data set is so small, pick the sorting algorithm that is the easiest to implement .. (hint bubblesort). Other fancy sorting algorithms don't show any significant performance until you have significantly large data set.

Here's a reference

http://mathbits.com/...rays/Bubble.htm

This post has been edited by jjl: 02 October 2012 - 10:50 PM

Was This Post Helpful? 0

### #5 obviousninja

• D.I.C Head

Reputation: 3
• Posts: 68
• Joined: 17-February 10

## Re: Find the Median

Posted 03 October 2012 - 01:59 PM

nvm, other algo works just as well.

This post has been edited by obviousninja: 03 October 2012 - 01:59 PM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }