This tutorial extends the program we did when taking our first steps with arrays. You probably won't need to go back to that. But visit it if you have any difficulty with what is in the tutorial you are reading now.
The only new material which this tutorial adds is the use of DATA and READ to fill the Q$() and A$() arrays. (The exercise's questions and their answers
The lines starting with the keyword DATA are "hidden" from users of the program.
They are there to supply data to any READs in the code.
If you said READ A$, the program would take a word or phrase from a DATA statement.
The first READ takes the first word or phrase from the first DATA. The next takes the next, and so on.
READ and DATA can also be used to fill numeric variables...
DATA 23,6 DATA 100 READ A%, B%, C% PRINT A% PRINT B% PRINT C%+34
... would print 23, 6 and 134.
The following shows you (I hope!) why I said that READ can pick up a word or a phrase...
DATA apple, mandarin orange, pear READ A$, B$, C$ PRINT A$ PRINT B$ PRINT C$
... would print...
apple mandarin orange pear
The program below shows you READ and DATA in action.
REM bas7-data keyword.bbc REM vers 01 Jul 2025 REM started 25 Feb 25 DIM Q$(4),A$(4) REM DIM Q$(4) creates Q$(0),Q$(1),Q$(2),Q$(3),Q$(4)... but NO Q$(5)! REM It creates FIVE Q$ array elements... the first being Q$(0) DATA France,Paris,Italy,Rome,India,Delhi DATA China,Beijing,Peru,Lima QsInSet%=5 Index%=0 REPEAT READ Q$(Index%),A$(Index%) Index%=Index%+1 UNTIL Index%=QsInSet%-1 PRINT "Enter 999 at any time to finish" WhichQLast%=999 REM What this is for will become clear in a moment, I hope REPEAT REPEAT WhichQ%=RND(QsInSet%-1) REM RND(4) Creates 0,1,2 or 3... but not 4 UNTIL WhichQ%<>WhichQLast% WhichQLast%=WhichQ% REM PRINT WhichQ% REM For testing program. PRINT "What is the capital of ";Q$(WhichQ%); INPUT Ans$ IF Ans$="999" THEN PRINT "Goodbye" WAIT (90) QUIT ENDIF IF Ans$=A$(WhichQ%) THEN PRINT "Right" IF Ans$<>A$(WhichQ%) THEN PRINT "Not right" PRINT UNTIL FALSE REM CHALLENGE: Add scoring, and a report of score at end. REM CHALLENGE: Get program to provide right answer when user does not REM CHALLENGE: Add code to let user have a second attempt REM CHALLENGE: Make the user type the RIGHT answer if he/she had to be told
The code above "works", but to make it more flexible, you could make changes as follows. First change the relevant bit so it reads
ArrayElements%=4 DIM Q$(ArrayElements%),A$(ArrayElements%) REM DIM Q$(4) creates Q$(0),Q$(1),Q$(2),Q$(3),Q$(4)... but NO Q$(5)! REM It creates FIVE Q$ array elements... the first being Q$(0) DATA France,Paris,Italy,Rome,India,Delhi DATA China,Beijing,Peru,Lima QsInSet%=ArrayElements%+1 REM +1 because pf element(0)
There's no way yet to know why I've added the ArrayElements% variable.But, inpart, what it does should be clear enough. Not clear? Look at the code again! Figure it out. What is it doing? What has it replaced? (You'd still need the rest of the original, of course!)
So far, we've written the code on the assumption that if someone wants to change the questions, they will put how many questions there are into the code at...
QsInSet%=
But we won't need that after the rest of these changes.
---
Add "5" at the start of the first data line. Make it...
DATA 5,France,Paris,Italy,Rome,India,Delhi
(Don't try to run the code again yet.)
And change ArrayElements%=4 to
READ ArrayElements%
Now you can change the questions in the exercise, AND change the number of questions easily.
Yes! You cold to that before, too... but not as easily. And if something is not easy, errors find it easy to creep in.
Also, eventually you will learn how to create external data files... the code to give users the exercise won't need any changing to change what questions must be answered. And you can have a "library" of question sets! When you learn about external data files.
I'm not explaining these two sentences at this time. See if you can figure out what they mean! (The come from the excellent BBC BASIC Manaual by R.T. Russell.)
RESTORE can be used at any time in a program to set the line where DATA is read from.
RESTORE on its own resets the data pointer to the first data item in the program.
You can use DATA and RESTORE perfectly well without using restore... but if you learn how to use RESTORE, more things become possible!
----------------------------
At one point I had...
WhichQ%=RND(QsInSet%)-1
... which ALMOST worked, mostly worked. It needed to be...
WhichQ%=RND(QsInSet%-1)
Try to figure out why one worked, and what was going wrong when I used the other.
With puzzles connected to RND, it is best to look at what happens when the RND returns the smallest number it can and what happens when it returns the biggest it can. Get the "ends" right, and the middle will probably work, too.
----------------------------
If you answer "rome" when the right answer is "Rome", the code calls your answer wrong.
If you don't care if users answer "rome", then the easiest answer is to make the DATA lines "Italy,rome,France,paris..." etc, and just after the INPUT Ans$, have the code change every letter in the answer to lower case. A good challenge for you! Look for your answers online!
Please get in touch if you discover flaws in this page. Please mention the page's URL. (wywtk.com/prgm/bas/bas7-data-keyword.htm).
If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.
|
Unlike the clever Google search engine, this one merely looks for the words you type, so....
* Spell them properly.
* Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....
Please also note that I have four other sites, and that this search will not include them. They have their own search buttons.
My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org.
Why is there a script or hidden graphic on this page? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Neither my webpages nor my programs incorporate spyware, but if the page has Google ads, they also involve scripts. Why do I mention the script? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....