Page 1 of 1

finding the mode

Posted: Sat Jan 03, 2004 7:22 am
by nathanj
If i had five numbers say 40, 30, 40, 50, 40
how could I find the mode using an array ???

Re: finding the mode

Posted: Fri Feb 06, 2004 5:41 pm
by Artie
What do you mean by "mode"?

Re: finding the mode

Posted: Wed Feb 11, 2004 12:10 pm
by yacinator
incase you didn't go to school you should know that it means the most frequently occurint number in a list  :P

Re: finding the mode

Posted: Wed Feb 11, 2004 3:41 pm
by Artie
incase you didn't go to school you should know that it means the most frequently occurint number in a list  :P
Heh-heh . . . your teachers must be proud.   ;)

Anyway, here's a code that works:

-----------------------------------------------------
CLS
DEFINT A-Z
n = 5'     How many numbers you have.
n = n - 1
DIM a(n, 1)
FOR x = 0 TO n
 READ a(x, 0)
NEXT x
FOR x = 0 TO n
 FOR y = 0 TO n
   IF x <> y THEN
      IF a(x, 0) = a(y, 0) THEN a(x, 1) = a(x, 1) + 1
   END IF
 NEXT y
NEXT x
FOR x = 1 TO n
 IF a(x, 1) > a(x - 1, 1) THEN t = a(x, 0) ELSE t = a(x - 1, 0)
NEXT x
PRINT USING "Mode: ####"; t
END

DATA 40,30,40,50,40
-----------------------------------------------------

Try this, Artie

Edit: Neatened up code a bit.