Thanks for the feedback on the series so far! Most of you have been following well and are now ready to turn our mini-application into something a little more polished.
The most obvious thing wrong with the current program is that it doesn't check if any of the random whole numbers generated are the same. In a real lottery draw, all the numbers (obviously) have to be different from each other and it's our task now to find a way of putting this check into our code.
If you think about it, before the program can check the numbers are different to each other, it first needs to 'remember' each number as well as just print it to the screen. A good way of remembering a sequence of numbers is to use an 'array'. Think of this as several similar integer variables all strung together in a line.
Type the following line near the top of the program:
LOCAL numbers%(6) rem Array to store picked numbers
While you're up at the top of the code, add ,j% after the section LOCAL
i%, as we'll need another counter later on. Your screen should now look
something like this:
Note how the character "%" after the variable name tells the program what type of variable the array represents and the number in the brackets tells it how many adjacent numbers need to be stored. In effect, the array's six elements can be imagined as:
numbers%(1) | numbers%(2) | numbers%(3) | numbers%(4) | numbers%(5) |
numbers%(6)
As each number is picked by the random number generator, we'll check for
duplications and then store it in one element of this array for safe keeping.
The check for duplications will need another small 'loop' (hence the j% counter
defined earlier). Here's the thinking:
Set a counter (j%) to 1
While j% is less than, or equal to the
number of ball numbers chosen so far
Go back to the 'While' line and test the condition again
Do you follow? Take a moment to think about how these loops work. I often find it helpful to work through a section of code one line at a time, writing down on paper the values of each variable and thus understanding what's actually going on. In doing this, you're effectively doing the job of the program, but a million times slower!! 8-)
Here's the code in OPL, please type in the lines shown in the screen show, changing existing lines as necessary. Sorry for the amount of typing, but it all needs to be entered if the program is to work at all!
Once you're happy you've entered the lines exactly as shown, I'll go through the changes we've made:
Go ahead and translate and run the program as usual. If all is well, you'll see the column of six numbers and hopefully all of them will be unique. Hooray! If you get an error, go back and check you've entered the code exactly as I've shown in the screen shot.
A little light relief is now needed from all this number-crunching. The most enjoyable part of any program is doing the titles and graphics, and it's high time we added something fancy to our application.
The first thing it needs is a title. What about "Milo" (short for My Lottery program)? Move up near the top of the program and insert the lines:
PRINT "Milo v0.1 (C) Fred Bloggs 1998"
PRINT "Here are the numbers for the next lottery:"
And similarly, at the end of the program, just before the line "GET", insert this PRINT statement:
PRINT "Press any key to exit"
Again, translate and run the program and you should see the following output:
Program Milo is now functional but awfully boring to look at. Next week, we're going to take this 'bare-bones' functionality and give it a pretty interface that you'll be proud to show off to others. See you then!
Go to next lesson | Programming index