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"
Help with routines choosing unrepeated data from an array
Fixed it myself
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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.
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
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"
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
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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