Page 1 of 1

Help with routines choosing unrepeated data from an array

Posted: Fri Jan 07, 2011 7:22 am
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"

Fixed it myself

Posted: Fri Jan 07, 2011 9:37 am
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

Posted: Fri Jan 07, 2011 1:34 pm
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

Posted: Fri Jan 07, 2011 2:26 pm
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

Posted: Fri Jan 07, 2011 3:31 pm
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