Module 4 - Game Skills 4
Assignment 4.09


Objectives: After studying the Module 4 Game Skills 4, you will be able to:

  1. Create a program using READ - DATA statements to enter sets of data.
  2. Use INPUT statements as needed to make the program interactive.
  3. Calculate the total sets of data and a the grand total.
  4. Use LOCATE and PRINT USING to control the horizontal placement of numeric and string data in a table.

YOUR LESSON

Introduction

As you have seen, READ - DATA is the most efficient way to deal with sets of data within a program. And, PRINT USING and LOCATE are the best solutions for precisely controlling printing on the screen.

What else can we add to the programs you have written in Module 4? How about INPUT? Remember that INPUT allows you to create interactive programs.

Before you get confused about all of these programming techniques for getting data into variables, let's consider some general guidelines for when to use READ - DATA, INPUT, and LET.

  • READ - DATA: This is a fairly easy decision. It is appropriate to use READ - DATA when you have sets of data that will not need to be changed during the course of a program.
  • INPUT and LINE INPUT: This is another easy decision. When the program requires interactivity, you have no choice, you must use either INPUT or LINE INPUT. The LINE INPUT statement is a little more limiting than the INPUT statement, so the purpose of the program will dictate which one you use.
  • LET: Well, another easy decision! Anytime you are going to perform a calculation, you must use an assignment (LET) statement. LET is also appropriate when you want to assign values to a few individual variables.

Was that easier than you thought? Those seem like some pretty simple general guidelines to follow; however, there are always exceptions! Here is a big one to consider.

If you had to process some kind of data (like a mailing list) for all of the students in a school, it would really clutter up a program to put all of the information in READ - DATA statements. Besides, addresses often change, so you would have to change the DATA statements in the program every time that happened. Consequently, READ - DATA would not be a good choice for such a program. This kind of situation is generally handled by storing data on a diskette or hard disk. When changes occur, the information on the storage medium is updated. When the information is needed, it is read into the program from the storage medium and used to perform the task. QBasic has the ability to read and write to storage media, but that is beyond the scope of this course.

On the other hand, sometimes you might be faced with the dilemma of whether to use LET or READ - DATA in a certain situation. After all, they basically accomplish the same thing--the assignment of data to variables. What's a programmer to do?

Maybe the easiest way to decide is based on the number of variables being assigned. Above, we discussed that it is appropriate to use LET when you want to assign values to a few individual variables. The key distinction here is few. If you had a program that required the assignment of "many" variables, you could either use a separate LET statement for each variable as we have been doing, or you could use a READ - DATA statement; however, it is hard to define what "many" would be. That is probably an individual decision that will be based on how much "clutter" you can stand in the program vs. how "well organized" you like your programs to be. When the "clutter factor" of using too many LET statements starts to detract from the program's organized appearance, it is probably time to switch to READ - DATA. (This is probably a good rule of thumb with regard to how well you keep your room at home picked up as well!)

Let's take a look at two quick examples and then tackle a new program.


Practice Time

Remember the Bake Sale program example? The data assignment section is shown below.


REM Assign Data
LET cake = 8.25
LET pie = 7.75
LET cookies = 6.25
READ tch1$, P1, P2, P3, P4
READ tch2$, J1, J2, J3, J4
READ tch3$, A1, A2, A3, A4

(the rest of the program would be in here)

DATA Mr. Putney,15,13,18,17
DATA Mr. Jordan,17,19,13,15
DATA Mrs. Atwill,23,20,41,16
END


To continue with the discussion of exceptions, let's take a look at some ways to modify this part of the program. Even though the variables cake, pie, and cookies aren't a set of data, there really is no reason why we couldn't use READ - DATA to assign their values. The following modification shows how this could be done.

REM Assign Data
READ cake, pie, cookies
READ tch1$, P1, P2, P3, P4
READ tch2$, J1, J2, J3, J4
READ tch3$, A1, A2, A3, A4

(the rest of the program would be in here)

DATA 8.25,7.75,6.25
DATA Mr. Putney,15,13,18,17
DATA Mr. Jordan,17,19,13,15
DATA Mrs. Atwill,23,20,41,16
END


You should reload the example and make these modifications yourself. The only thing to keep in mind is that the variables cake, pie, and cookies are now in the first READ statement, so their corresponding values have to be in the first DATA statement. If you don't keep them matched up (remember one-to-one correspondence), the program will not run; at some point, numeric variables would be mismatched with string data and vice versa. Ooops!

Another possible modification is to make the Bake Sale program interactive. This could be done in several ways, but we will just consider a simple possibility. What if we asked for the price of each item to be input from the keyboard instead of fixing each price at 8.25, 7.75, and 6.25? This could be done as follows.


REM Assign Data
INPUT "Please enter the price of the cake: ",cake
INPUT "Please enter the price of the pie: ",pie
INPUT "Please enter the price of the cookies: ",cookies
READ tch1$, P1, P2, P3, P4
READ tch2$, J1, J2, J3, J4
READ tch3$, A1, A2, A3, A4

(the rest of the program would be in here)

DATA Mr. Putney,15,13,18,17
DATA Mr. Jordan,17,19,13,15
DATA Mrs. Atwill,23,20,41,16
END


You should also make these modifications yourself and observe how the program runs.

Be sure to study both examples carefully.


YOUR ASSIGNMENT

The program in this assignment is designed to give you more experience in the following areas:

  • using READ - DATA statements to enter sets of data
  • creating calculation statements
  • controlling the appearance of output with PRINT USING statements
  • calculating totals

Details of this assignment are provided in the following pdf file which you should print out and carefully study.

  • Download and print this file now: 4_9_1.pdf

Submit your completed assignment "Assignment 04.09" to your instructor for a grade in the Dropbox area.


A QBasic Review

Before you move on to the next assignment, please make sure that you can do the following.

  1. Create a program using READ - DATA statements to enter sets of data.
  2. Use INPUT statements as needed to make the program interactive.
  3. Calculate the total sets of data and the grand total.
  4. Use LOCATE and PRINT USING to control the horizontal placement of numeric and string data in a table.

You are learning how to put the pieces together to make simple, efficient programs. Keep up the good work!


After you complete this assignment, return to the GameBoard and move on to Assignment 04.10.

previousnext