Hello,

I am trying to write a template for finding the middle of 3 values, and I am going to call it the "median". For example, the median of {1, 0, 2} is 1. The median of {"abc", "ghi", "def"} is "def". Any help would be great.

Thanks Frank

Printable View

- 10-28-2001frankmedian
Hello,

I am trying to write a template for finding the middle of 3 values, and I am going to call it the "median". For example, the median of {1, 0, 2} is 1. The median of {"abc", "ghi", "def"} is "def". Any help would be great.

Thanks Frank - 10-28-2001Stoned_Coder
if you are only having 3 inputs then all you need are a couple of comparisons to see which one is greater than one but not greater than another...

- 10-28-2001frank
Right, I understand if it is just integers. But what if it is like the alphabet example. abc def ghi, where def is the middle one?

Thanks - 10-28-2001Stoned_Coder
if they are stl strings then you can compare them the same way but old c-style strings you will need to use strcmp()

- 10-28-2001Unregistered
place the strings in an array.

sort the strings into alphabetical order

determine which index of the array is the median index

the string stored in the median index of the array is the median string.