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.

# Find the Median

Page 1 of 1## 4 Replies - 2512 Views - Last Post: 03 October 2012 - 01:59 PM

##
**Replies To:** Find the Median

### #2

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

### #3

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

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.

### #4

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

Here's a reference

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

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

### #5

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

Page 1 of 1