finding the mode

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
User avatar
nathanj
Newbie
Posts:1
Joined:Sat Jan 03, 2004 7:16 am
finding the mode

Post by nathanj » Sat Jan 03, 2004 7:22 am

If i had five numbers say 40, 30, 40, 50, 40
how could I find the mode using an array ???

Artie
Jr. Member
Posts:23
Joined:Fri Feb 06, 2004 12:48 am
Location:Florida
Contact:

Re: finding the mode

Post by Artie » Fri Feb 06, 2004 5:41 pm

What do you mean by "mode"?

User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am

Re: finding the mode

Post by yacinator » Wed Feb 11, 2004 12:10 pm

incase you didn't go to school you should know that it means the most frequently occurint number in a list  :P

Artie
Jr. Member
Posts:23
Joined:Fri Feb 06, 2004 12:48 am
Location:Florida
Contact:

Re: finding the mode

Post by Artie » Wed Feb 11, 2004 3:41 pm

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.

Post Reply