[ back to toc ]


Date: 2002/03/21 10:00

I have an assignment to complete in my C programming class that requires
me to create an array of 11 numbers by reading them in, sorting them, then
calculating the mean, median and mode of the 11 numbers and printing out
the results. I have everything done except the mode. I'm sure I need a
loop with a counter, but I just can't figure out the logic. I'm using
Borland Version 5.02. Thanks.
To get the mode is much more difficult then the median or mean value. Mode
is the valuse that appears the most. To do that you have to allocate two
arrays, call it 'count' and 'value'. To be on the safe side, you can
allocate the length for the worst case, that is 11 elements.

Have a variable, call it 'index' that is zero at start and allways points
to the first free element of the arrays 'count' and 'value'.

Iterate over the values of the numbers and when taking the next number
search the array 'value' if the actual value is stored there. The search
(search iteration variable can be named 'i') should go from zero up to but
excluding 'index'. If the actual value is 'value[i]' equals to the actual
value, youshould increment 'count[i]++'. If the search is not successful,
in other words if this value never appeared so far you have to store the
value in 'value[index] = thisvalue' and set 'count[index] = 1' and finally
increment 'index++'.

When the iteration all over the numbers is finished, you should search for
the largest number in array 'count' and print the corresponding 'value[]'.
There may be more than one maximal elements.


[ back to toc ]