Section 4 - Variables, Input, Output In all of our examples so far we have placed the necessary data on the stack before the program starts. This works but it can create problems for both the programmer and the user. This is best demonstrated by Problem 3 of Exercise Set 2; manipulating the stack with five values is somewhat of a challenge for the programmer, and the user must remember the exact order in which the data must be entered for the program to work correctly. It would also be helpful to the user if programs like BWL2, which have several results, had each output item labeled. In this section we will learn about the use of variables, which helps reduce the amount of stack manipulation that may be required by a program, and we will learn about some input and output techniques that will make the programs more user-friendly. There are two types of variables available for programming, global and local. The variables you learned to use on pages 2-46 through 2-60 of UG are global variables. They function in programs just as they do in normal operation. Local variables
are specific to a program and live only while the program is running. We will adopt the convention of using upper case
letters for global variables and lower case letters for local variables. To create local variables you must first put the values on
the stack, put in an arrow, The problem of forcing the user to memorize the order in which data must be entered can be solved with the INPUT
command. This command allows the program to prompt the user for the data one item at a time. To use the command there
must be a prompt (a string telling the user what to enter) on level 2 of the stack, "" on level one of the stack, then the
INPUT command, which can be found at LS PRG NXT F5-IN F4-INPUT. When the command is executed the program
pauses with the prompt displayed at the top of the screen and the cursor set to the command line ready for input. When the
user presses ENTER the program resumes and places the input on level 1 of the stack as a string. Because the input is a
string the most common command to follow the INPUT command is
the We will discuss two methods of labeling output, tagged output and the message box. Tagged output is generally used at the end of a program. It leaves one or more output items on the stack, each with an explanatory tag. The message box is generally used to pause the program with intermediate results during program execution. After a message box comes up, the user must acknowledge it by pressing F6-OK for the program to continue. To create a tagged output item the output object should be on stack level 2 and the desired explanatory tag on level 1 as a
string. Then the << ... "There are " SWAP + " apples in the bag." + MSGBOX ... >>. Notice that it was not necessary to convert the number to a string; when + operates on a string and a number it converts the
number into a string then concatenates. If, however, you wish to concatenate two numbers, you must first explicitly convert
at least one of them to a string with LS PRG F5-TYPE We will now consider two new versions of our bowling program; the first will demonstrate the use of global variables and the message box and the second will demonstrate the use of the INPUT command, local variables, and tagged output. For the first example, which we call BWL3, we will make the following assumptions. During the play of the games we were using a score keeping program that left the scores of the three games in the calculator memory as SCR1, SCR2, and SCR3, and we want BWL3 to leave the new handicap in memory as HNCP for use by the score keeping program next week. With this in mind, the coding for BWL3 is
<<
SCR1 SCR2 + SCR3 + "Series total = " OVER + MSGBOX
3 / FLOOR "Average = " OVER + MSGBOX 200 SWAP - 0 MAX
.8 * FLOOR "New handicap = " OVER + MSGBOX 'HNCP' STO
>>
Try single stepping through this program to see exactly how it works. Notice that you will have to create the global variable SCR1, SCR2, and SCR3 before you can run the program. It is not necessary to create HNCP in advance, the calculator will create it if it does not exist, and will overwrite the existing version if it does exist. The second example, which we will call BWL4, will use the INPUT command, local variables, and tagged output. Admittedly, the use of local variables in this case is not particularly helpful, but it does provide an example. Unlike BWL3, this program is intended to stand alone. HINT: As you enter this program make use of the COPY PASTE feature to simplify entering the first three lines.
<<
"GAME 1 SCORE" "" INPUT
Notice that to create the local variables tot, avrg, and hncp it was necessary to put an initial value of 0 on the stack for each of them. The initial value did not have to be zero, it could have been anything, but each must be initialized. Again, try single stepping through this program to see exactly how it works. The HP49G+ has several other input and output features that will not be discussed in this text. Those interested in these more "exotic" I/O forms should consult pages 21-19 through 21-43 of UG.. EXERCISE SET 4 Use the editing suggestions from Section 3 to make the required changes.. 1. Change Problem 2 of Exercise Set 2 to add tags to the three lines of output. Save the new version as TAX3. 2. Change Problem 1 of Exercise Set 2 so that the desired exit number and the speed limit are obtained from global variables
and the current mile marker is entered with an INPUT command. The distance and time should be reported by way of
message boxes. The message for the time should read something like "Time = 2 hours and 12 minutes." There are two
functions that should help make your output "pretty." One is LS MTH F5-REAL NXT NXT F1-RND (see page 3-14 of
UG). The other is LS CONVERT F4-REWRITE NXT NXT
3. Change Problem 3 of Exercise Set 2 so that the values of x1, y1, x2, y2, and x are entered with INPUT commands and are assigned to local variables. The output, y, should be tagged. Save the new version as LIN2.
|