sisco311 said: [thread=717011]Homework?[/thread]
The arithmetic mean of a list of numbers is the sum of all of the list divided by the number of items in the list. ;)
Although your math is perfect, the OP is looking for the median, not the mean. :D
I don't use awk unless I absolutely have to. There are a zillion ways to force a computer to find the median. OpenOffice.org Calc can certainly do this, although it's hardly a unix command line application. It's not overly difficult to write a simple script in python to find the median value, but you do have to be careful. Being lazy, I would not bother writing a script to calculate the median value of a list. Instead, I would use a program like R to calculate the median. Another option is PSPP. Since you specifically asked for "command-line", I'll show you how to do this in R. First, we will need to install it.
sudo apt-get install r-recommended
You can start R with the command . . . . . "R" at the command prompt. The following syntax will return the median value in a list. [INDENT]> options(STERM='iESS', editor='emacsclient')
example <- c(10, 15, 25) median(example)
[1] 15 [/INDENT]Let's break this down. The first line isn't necessary. It just tells you that I use Emacs to interact with R. Lines 2, 3 are actual R syntax. Row 4 is R telling me that 15 is the median.
Of course, your example is the easiest possible case, because it has an odd number of elements. Here's another example. [INDENT]> example <- c(10, 15, 25, 17)
median(example)
[1] 16 [/INDENT]Median is a funny thing. Used correctly it _can_ be a useful measure of central tendency. But, it can also be kinda funny and mis-leading. As a policy analyst, I often use it to compare things like income, because it is not affected dramatically by outliers in the same way the mean is.
If this is for a school assignment, I would be curious to learn where / what class. I do enjoy it when professors ask their students to use command-line tools. For extra credit you can sit down tomorrow and show me how to calculate the median in PostgreSQL or MySQL.