If you are new to programming? I suggest you read the topic 'The VERY VERY VERY Basic info you NEED TO READ FIRST
' before you continue with this lesson. It can be found by clicking here
and gives you a very simple,detailed description of what you need to know to follow these lessons.
A simple quiz.
We are nearing the end of this introductory series of tutorials on the concept of programming, so time for a little relaxation i think?
By now you should have a fair grasp of the principles involved in writing programs, and you will be more confident in attempting to create your own programs to do whatever you wish. As i mentioned previously, there are many flavours of BASIC and not all instructions are compatible with all versions. The following program is an example of this and won't run on the usual site link that you have been using up to now.
Cast your mind back to the very first program we wrote, the simple artificial intelligence program, we will be working along broadly similar lines here to write a program that creates a quiz. Creating a quiz is a fairly straightforward matter of creating a loop for how many questions you wish to ask, then reading from data the question and answer, followed by asking the user for their answer, then comparing it with the stored answer that we read. When the loop finishes then the quiz ends.
You can of course add different features if you wish? Features could include such things as: Printing titles, Confirming whether the answer was correct or not?, Keeping a score of correct answers to print at the end, And having a High Score facility, etc.
Right let's get started on our quiz program, we wish to print our questions in a random order, so that the quiz doesn't get too boring for those who use it frequently, also, because it's asking the questions randomly, we need to ensure that the same question is not asked twice in one session.
20 let count=0
30 dim z(10)
40 for a = 1 to 5
50 let n=int(rnd*10)+1
60 for x = 1 to n
70 read c$
80 read d$
90 next x
110 if z(n)<>1 then 130
120 goto 50
130 let z(n)=1
140 print c$
150 input a$
160 if a$=d$ then let count=count+1
170 next a
180 print "well done you got " ; count ; " right"
190 data who developed windows,microsoft,what do nokia make,phones,who makes the silver spirit,rolls royce,what product aids recovery,lucozade,capital of australia,canberra,fourth planet from the sun,mars,who sung metal guru,t rex,largest ocean is,pacific,longest river is,nile,the us space agency,nasa
So how does it work?
Line 10 Randomize, this merely tells the computer to randomly select a set from the random number sets that it creates, in other words make it as random as possible.
Line 20 let count=0 we will be using 'count' to count the number of correct answers the user gives, so we set it to 0 initially
Line 30 in this simple program i have 10 questions, and each time a question is asked we need to 'flag' the question number so that we know that that question has been asked before and can be ignored if it comes up again. I use the array 'z'and dimension it to 10, of course this figure will alter depending on how many questions you have stored as data, if you have 500 questions, then Dim z(500) would need to be used
Line 40 is the control loop that controls how many questions are asked, here it is set to 5. If you wished to ask 20 questions for instance, then you would need to make sure that you had at least 20 questions & answers in the data statement, then alter the loop to read 'For a=1 to 20'
Line 50 Generates a random number between 1 and 10 and stores it in variable 'n'. We have covered random number generation in previous lessons, so i'll not go into detail here, except to say, that the Rnd*10 part here, is different to the Rnd(1)*10 part we used previously in the other version of BASIC. Also you would use the total number of questions stored rather than 10, eg. if you had 500 questions then you would need to put Rnd*500, instead of Rnd*10.
Line 60 this is a loop within a loop (nested loop) all this loop does is read data, ie. the question and answer, this is what makes the quiz random, for instance if the random number generated was 7, then n would equal 7, the loop would read 7 questions and answers, so that when the loop finished question 7 would be in c$ and answer 7 in d$. The loop ends at line 90
Line 100 because the data has been read 'n' times,(as above example) we need to restore the data pointer so that it starts from the beginning again
Line 110 this looks at our array, 'z' has 10 positions in our example, (this would vary according to how many questions you had in your quiz) it looks at position 'n' in the array, and checks to see if it's not equal to 1, (we let that position equal 1 when the question has been asked) If it's not equal to 1 then it jumps to line 130.
Line 120 is a simple goto command,and will be executed only when position 'n' in the 'z' array is 1, (in other words that question number has already been asked) in which case it jumps back to line 50 to choose another number and repeats the above process.
Line 130 this line is executed when a question number hasn't been asked before, ie. array 'z' position 'n' (z(n)) is not equal to 1, so if we refer back to our above example, and 7 was the random number generated, then it would set the 7th position in the array to 1, thus telling the program that the 7th question has been asked. This would mean that if 7 came up again as a random number, the program would ignore it and choose another. (lines 110-120 above)
Line 140 simply prints the question that was read into c$
Line 150 accepts the answer into a$
Line 160 checks to see if the answer in a$ is the same as the answer which was read from the data in d$, if it is then it adds 1 to 'count' which is our total of correct answers.
Line 170 completes the loop for the number of questions to be asked, (in this case 5) when all 5 questions have been asked, then the next instruction (line 180) will be executed
Line 180 simply prints out the end message and prints 'count' to let you know how many you got right
Line 190 is the data which holds the questions and answers seperated by commas, Note that here no "" marks are entered on the characters as they were previously, some versions of BASIC require them, some don't.
The data takes the form, question1, answer1, question2, answer2, and so on.
Line 200 ends the program.
This program uses instructions that will not work on the usual link so copy/paste it will not work there, but i've included it for you to try in your own BASIC compilers.
If you wish, you can test this program or your own here: Test your BASIC programs.
Simply click the link above and then select the 'write your own' tab, you can then type your program in or copy/paste from here or notepad for example. You can also modify this program by copy/paste and editing it there.
I hope you have enjoyed this tutorial, I will be making more in the future and covering other projects, slowly building up your knowledge and confidence, so that you can start to build your own programs for your own needs.Click here to proceed to lesson 16.
If you have enjoyed this tutorial please hit the thumbs up button at the top right hand side of the post to say thanks.
If you wish? you can view or download the original BASIC programming manual here: BASIC Programming Manual
Why not take a look at the complimentary video showing how this program runs?