3-Lib banner

The Absolute Beginners Guide to OPL

Part 3 - Taking shape!


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:

Screen shot

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!

Screen shot

Once you're happy you've entered the lines exactly as shown, I'll go through the changes we've made:

  1. Rather than just print out the random whole number, we've stored it in an element of our numbers%() array, i.e. we've 'remembered' it for checking in a moment or two.
  2. We've inserted another loop (using j%) within our existing loop (i%). This is just the checking process described above written out in proper programming terms.
  3. Note that the j% loop only goes up to (i%-1). In other words, when we're picking the first number (i.e. i% is 1), the j% loop never even gets started, as its WHILE condition fails immediately. On the second lottery number (i% is 2), the j% loop gets done once. Think about it, the program is checking whether numbers%(2) is the same as numbers%(1). If we were in the middle of picking the fifth ball (i.e. i% is 5), the j% loop would go up to 4, thus checking the new number against each of the four balls picked so far. And so on, I'm sure you get the idea.
  4. If a duplicate is found, the program is told to return to the line labelled "picknum::", where of course the random selection happens all over again. You can use any names you like for program line labels, but it helps to make them memorable to help you understand the code a few weeks hence. Note that using GOTO is usually considered a bad programming technique by experts, but don't let that worry you for now. Our code may be inelegant but it works, and quite simply at that.
  5. Note that we now PRINT out the value of each chosen number right at the end of the i% loop, by which time we know that it's not a duplicate of an existing number.

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:"

Screen shot

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:

Screen shot

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