Loading and Processing Data
KeyWords: READ...DATA, GOTO
As promised at the end of Chapter Three, here are the answers to the two problems you were supposed to try. You did try them, didn't you? Here is the program to compute the factorial of 9 (The names for your numeric variables will probably be different):
LET PRODUCT = 1 FOR NUMBER = 9 TO 1 STEP -1 LET PRODUCT = PRODUCT * NUMBER NEXT NUMBER PRINT "9 Factorial is ", PRODUCT END
We will learn a slightly better way to print the output later in this chapter. All we do is get rid of the comma in line 5. Here is the program listing for the last problem, adding the numbers from 1 to 750 (1+2+3+4+5+6+...+747+748+749+750=???):
LET SUM = 0 FOR NUMBER = 1 TO 750 LET SUM = SUM + NUMBER NEXT NUMBER PRINT "Sum Is", SUM END
You may have noticed that the last program takes about 1/4 second to run. It may seem like it's pretty slow, but then ask yourself: can you count from 1 to 750 and keep a running total in your head in a quarter of a second? Probably not! In fact, go ahead and type in that last program listing above. We'll be using it to learn about that PRINT trick. I'll wait here while you type.
Oh, good! You're back! Go ahead and run the program. Now change line 5 To:
PRINT "Sum Is" SUM
Now run the program again. Notice how the answer is moved much closer to the words? Makes it easier to read! Incidently, QBASIC is assuming that there is a semicolon (;) between the string constant (the message in between the quotes in the PRINT command) and the variable SUM. In fact, you may have noticed that QBASIC automatically inserted a semicolon. If you want to print the values of two variables next to each other, you would have to use the semicolon. It would look something like this:
PRINT A; B; C
If you type that in the immediate window, you would get three zeros on the same line, with two spaces between adjacent zeros. One space comes before each digit, and is there to hold the place of the sign. If the number was negative, there would be a minus sign in that space. There is another space after the number just to separate it from other numbers that might be printed in the same PRINT command. It just makes the output look better by not running numbers together.
Now that we have that out of the way, let's process some data in our programs. Clear out the current program, and type this in:
READ L,W LET P = 2 * L + 2 * W PRINT "Perimeter =" P DATA 6.5, 2.3 END
Go ahead and run the program if you wish. Can you figure out what lines 1 and 4 are doing? For those of you who are totally baffled, it goes something like this: First off, the program sees line 1: READ L,W. When the computer sees a READ command, it looks for the first DATA statement in the program. Ah-HA! There it is, down in line 4! Let's see. It wants us to read L and W. Oh, look! There are two numbers in that DATA statement (They are separated by a comma)! Ain't that handy? So what happens? Well the first piece of data in the DATA statement (6.5 in our case) is assigned to the variable L, and the second piece of data (2.3) is assigned to the variable W. Incidentally, it doesn't matter where the DATA statement appears in the program, as long as it's before the END command. In other words, all of these three program listings operate identically:
DATA 6.5, 2.3 READ L,W LET P = 2 * L + 2 * W PRINT "Perimeter =" P END READ L,W DATA 6.5, 2.3 LET P = 2 * L + 2 * W PRINT "Perimeter =" P END READ L,W LET P = 2 * L + 2 * W DATA 6.5, 2.3 PRINT "Perimeter =" P END
The above program processes just one set of data. In our case, it computes the perimeter of only one rectangle. What if we have three rectangles we want perimeters for? Here is the program:
LET P=2*L + 2*W
PRINT "Perimiter =" P
DATA 6.5, 2.3, 7.86, 6.03, 21, 17
Oh, Look! A new command: GOTO. It does just what it says. When the program reaches line 5, it is told to go to the label TOP. Now it sees that READ command again. What does it do? Well, it remembers where it left off in the DATA statement! Since it read in the first two numbers the first time around, it is going to assign the third piece of data (7.86) to the first variable in the READ command (L) and the next data (6.03) into W. Every time it is asked to READ another value, it takes the next one from the DATA statement. It doesn't even have to be the same READ command; it is for any READ. What happens when it runs out of numbers in the DATA statement? Well, it's not pretty. Run the program and find out. See how you get an Out Of DATA error? Then the program just stops. Hey, it's not great, but it works. We're just learning here, we're not trying to be experts right off the bat! To make the error message go away, click on the OK. If you want to see what the program displayed, press the F4 key to display the output screen. Press any key to return to QBASIC. By the way, a good way to eliminate the "Out Of DATA" error is to have your first DATA element indicate how many times the loop will run (if you know that). Then you could start off your program with something like:
READ CYCLES FOR LOOP = 1 TO CYCLES ...
Another good way is to have two more data items of the number "-1" at the end of the DATA statement. Then after you READ the variables, immediately check to see if they are equal to -1 with an IF...THEN command. If it's true, place the label name before the END command after the THEN. Here's how it would look:
TOP: READ L, W IF L = -1 THEN GOTO NOMORE LET P = 2 * L + 2 * W PRINT "Perimeter ="P GOTO TOP DATA 6.5, 2.3, 7.86, 6.03, 21, 17, -1, -1 NOMORE: END
This is the preferred method by many programmers, as they don't have to determine how many times the program will run through the loop. Also notice that there are two -1's at the end of the DATA statement. Why? Because the READ command has to fill 2 numeric variables in this example. If we only have one -1 in the line, it will be assigned to the variable L, and there won't be anything for the variable W to be assigned, so you'll still get the Out Of DATA error message. Got it? Good!.
Let's learn something new about our friendly neighborhood PRINT command! All along, we've been using a LET command to do all of our calculating for us. Did you know that you can calculate in a PRINT command? Try it now: Type this in at the Immediate window:
and press Enter. See? It gives you a 9 as a result. That's because 72 divided by 8 is 9. PRINT will automatically calculate anything you tell it to before it prints out. The above program would look like this (You may enter and run it if you wish, but don't forget to erase the current program from memory!):
MORE: READ L, W IF L = -1 THEN GOTO QUIT PRINT "Perimeter ="; 2 * L + 2 * W GOTO MORE DATA 6.5, 2.3, 7.86, 6.03, 21, 17, -1, -1 QUIT: END
That got rid of our LET command, didn't it? We'll let you off easy on this chapter again, but here are some programs that you have to (try to) write before we give you the answers in the next chapter. Also next chapter, we'll show you how to have the program ask the user for data from the keyboard. We will also talk about and use "Nested Loops". Meanwhile, here are some programs for you to try to write.
READ in a series of 20 positive numbers, and print out the largest number. Make sure the program works no matter where in the DATA statement the largest number appears, and use the "-1" method to know when to stop reading data.
Find the average of two numbers. Use at least 3 pairs of data. Do not bother to test for end of data; let the computer generate an error message.
READ a positive integer N and print the product of the four consecutive integers N, N+1, N+2, and N+3. If the program runs correctly, the answer plus one will be a perfect square each time. Use at least five integers in the DATA statement, and do not bother testing for end of data.
That's all for now, See you next time!!!
Introduced In This Chapter:
Keywords: READ...DATA, GOTO
Concepts: Semicolons in the PRINT command, calculating in the PRINT command,
positioning of the DATA statement in a program, how data is read from
a DATA statement.