Help with routines choosing unrepeated data from an array

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
cubragol
Newbie
Posts: 3
Joined: Fri Jan 07, 2011 7:02 am

Help with routines choosing unrepeated data from an array

Post by cubragol »

Hi thanks for running these boards.
I am stuck trying to write a routine in a larger programme.
I want to choose data randomly from an array and write it to a new array. It doesn't matter which order it arrives in as long as data is not repeated in the new array.
I am not using days of the week but have chosen them as an example to hopefully make things clearer
The routine below allows repetition e.g. friday, monday, friday
How do i code it so that three unrepeated pieces of data will be chosen?
I have tried various ways with subroutines but keep coming unstuck.
Can anyone help?
Thanks in advance.

CLS
CLEAR
RANDOMIZE TIMER

DIM weekday$(7)
FOR a = 1 TO 7
READ weekday$(a)
NEXT a

DIM newlist$(3)

FOR z = 1 TO 3
pickday = INT(RND * 7) + 1
newlist$(z) = weekday$(pickday)
NEXT z

FOR a = 1 TO 3
PRINT newlist$(a)
NEXT a

DATA "monday","tuesday", "wednesday", "thursday","friday","saturday","sunday"
cubragol
Newbie
Posts: 3
Joined: Fri Jan 07, 2011 7:02 am

Fixed it myself

Post by cubragol »

:D
rather inelegant solution probably but here it is

dayprogram:CLS
CLEAR
RANDOMIZE TIMER

DIM weekday$(7)
FOR a = 1 TO 7
READ weekday$(a)
NEXT a

DIM newlist$(3)


DO WHILE z < 3
days:
GOSUB daypick
GOSUB daycheck
IF flag = 1 THEN GOTO days
IF flag = 0 THEN newlist$(z) = tempday$: z = z + 1
LOOP

FOR a = 0 TO 2
PRINT newlist$(a)
NEXT a

DATA "monday","tuesday", "wednesday", "thursday","friday","saturday","sunday"

INPUT ; a$
IF a$ = "y" THEN GOTO dayprogram
STOP

daypick:
pickday = INT(RND * 7) + 1
tempday$ = weekday$(pickday)
RETURN

daycheck:
flag = 0
FOR x = 0 TO z
IF newlist$(x) = tempday$ THEN flag = 1
NEXT x
RETURN
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

You don't need to take the values out of the original array. Just make sure the array elements are not in the same order. SWAP allows you to work with 2 different arrays at the same time by trading the values.

Code: Select all

DIM Index(7), weekday$(7)
RANDOMIZE TIMER
CLS
FOR a = 1 TO 7  'set days and index numbers in order
  Index(a) = a
  READ weekday$(a)
NEXT a

FOR i = 1 TO 7
  IF Index(i) = i THEN   'find elements same as index
    DO: j = INT(RND * 7) + 1: LOOP WHILE j = i   'loop until j is not = to  i
    SWAP Index(i), Index(j)   'swap 2 elements
    SWAP weekday$(i), weekday$(j)
  END IF
NEXT

FOR a = 1 TO 7  'check the order
  PRINT weekday$(a)
NEXT


DATA "Monday","Tuesday", "Wednesday", "Thursday","Friday","Saturday","Sunday"
The secret is to SWAP both arrays exactly the same way. I have used this type of procedure to arrange the alphabet so that no letter matches the ASCII code for an anagram puzzle program.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
cubragol
Newbie
Posts: 3
Joined: Fri Jan 07, 2011 7:02 am

Post by cubragol »

Thanks Ted I knew there must be a more elegant way.
I am so rusty.
I learned to program on a Sinclair ZX spectrum and I don't remember having access to the swap command!
Thanks again. :D
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Your welcome and I have no idea what a Spectrum is LOL!

The big advantage of doing SWAP is that you CAN'T lose anything already in the array. Well, at least it's harder to lose them lol.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply