4 Replies - 1260 Views - Last Post: 03 October 2012 - 01:59 PM Rate Topic: -----

#1 byron2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • 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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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