| 11-13-2008, 04:38 AM | #1 |
Hey I'm still new to the JASS language, but I was wondering if there was a way to track a median value for a set of integers (or reals). In my map I've got a set of 8 integers, and it would be helpful if I knew the median value. The only thing I can come up with is using a bunch of if-then statements. I haven't actually coded it, but I imagine it getting pretty ugly. Does anyone know of a way to get the median value of a set of integers? |
| 11-13-2008, 04:57 AM | #2 |
Assuming you are continuously sorting them: JASS:if(R2I(I2R(integercount)/2)==I2R(integercount)/2)then //or something... IF IT'S EVEN: return (stack[integercount/2]+stack[integercount/2+1])/2 else //it is odd: return stack[R2I(I2R(integercount)/2+.5)] endif I'm not actually sure that all the R2I's and such are needed... also there's probably an easier way to do it, but this is my attempt. |
| 11-13-2008, 08:04 AM | #3 |
No no, im sorry maybe I worded it funny. I didn't mean the mean value. I meant the median. The number thats in the middle. Take for example the set {1, 6, 9, 11, 75} the median is 9 because its the middle value. anyway to locate that integer without creating a BUNCH of if-then statements? |
| 11-13-2008, 09:08 AM | #4 |
No, you need a bunch of if-then statements. However, you sort them as numbers are added to the set so that there are less to do at any given time. |
| 11-13-2008, 12:33 PM | #5 |
If all of your numbers are sorted from least to greatest, then the above way should work (mine). |
| 11-13-2008, 02:39 PM | #6 | ||
Quote:
Quote:
If your list is sorted, Here-b-Trollz's code does exactly what you say you want. You could indeed shave a few I2Rs from it, though. JASS:if (count/2)*2==integercount then return (stack[count/2-1]+stack[count/2])/2 else return stack[count/2] endif This is assuming your values are sorted and stored in an array under indexes from 0 to count-1. |
| 11-13-2008, 04:59 PM | #7 |
Oh ok thanks, I'll try it out +rep |
| 11-14-2008, 08:59 PM | #8 |
