Wednesday, November 20, 2013

Simpleton Geek Teaches Computer Programming Book 2 Chapter 6C


6.3 LOCATE, CX,CY:INPUT + variable + array + pointer
“Does everybody have the designs in your journal?” The teacher was asking the students to show off their homework. The students answered affirmatively. “Okay, just hold them up and let me see them.” The teacher then walked around with the grade book and write down numbers. Just like that, it was over.
“I'm very disappointed with you. Didn't I say to fill the screen? Most of you did not fill the screen! When even the most basic requirement is not met, the best I can give you is 5 points out of 10. That's fail!”
The students were disappointed. “How do you expect to fill the screen?” The teacher said nothing but pointed to Peter's journal, which shows an ASCII art of a puppy. “See how there are characters all round the screen, including the right edge and the bottom? That's how you're supposed to fill the screen. In fact, the center may be blank for all I care, as long as the edges are filled, I'm happy. The way it is now, 2 points are given for doing your homework. 1 point each for each compass point filled. Most of you get 5 points. That's not filled screen. I want all edges filled. Like this:”
The teacher showed off his design, which is nothing but a bunch of Xs all along the edges. The inside of it is blank.
“See? It doesn't matter what you draw inside. It can be random. But you need to fill the screen! You failed at even that?” the teacher shook his head sadly. “Well, what's done is done! Let's move on.”
The students were all disappointed, but said nothing. It was their fault for not clarifying the requirements of the homework.
“Okay, now that you have the design, let's see you program it. It's easy. Just use a bunch of print statements on it. And remember, you can use question mark as PRINT command.”
As the student get busy working with their devices, Peter waved his above his head. The teacher approached him. Peter proudly showed his design on the screen already. He had anticipated the requirement and had done the work. The teacher smiled. Then he wrote a few more commands in the journal. LOCATE, CSRX, CSRY. Peter got busy with the help menu.
Other students were having trouble. “Teacher! I have blank lines when I print all the way to the screen!”
“Remember to put in semicolon if you don't want to print newlines after printing!”
“Sorry, teacher. I forget!”
The students worked hard, but eventually, the slowest of them managed to finish the task. The teacher has been standing on the board with this written out:
X Y Text
3 2 FRUIT
5 3 APPLE
5 4 BANANA
5 5 CHERRY
3 7 ICE CREAM
5 8 VANILLA
5 9 CHOCOLATE
5 10 STRAWBERRY
“Alright. Now I want you to look at these coordinates, and I want you to write print this text on the screen. You know coordinates, don't you?”
“Yes, teacher.”
“Then, assuming the border that I displayed last time, where do you think the text FRUIT will appear?”
The students counted 5 spaces right, and 2 spaces up, from the lower left corner. “Here, teacher.”
The teacher frowned. “Nope. That's not it. Try this:”
CLS:LOCATE 5,2:?”FRUIT”;
The students typed it into the device and came up with the text “FRUIT” displayed 5 spaces right and 2 spaces down from the top left corner.
“See? The 0,0 coordinate is at the top left, instead of the bottom left.”
The students were confused. “But we were all taught that 0,0 is on the bottom left!”
“Ha! That's mathematic! This is computer programming, and in computer industry, (0,0) is top left. Long complicated story. It started with CRT scans and video memory. In short, that's just the way it is. It's good that you have learned mathematical coordinates, but this is computer, and you have to adjust your learning to be able to handle new things. In fact, the ability to learn new things is paramount to your success. So, I'll be putting you into new thinkings all the time.
“So, now, we'll just program this thing in. However, instead of doing with by counting. We'll simply set the cursor to these locations. Like this:”
LOCATE 3,2:?”FRUIT”
LOCATE 5,3:?”APPLE”
LOCATE 5,4:?”BANANA”
LOCATE 5,5:?”CHERRY”
LOCATE 3,7:?”ICE CREAM”
LOCATE 5,8:?”VANILLA”
LOCATE 5,9:?”CHOCOLATE”
LOCATE 5,10:?”STRAWBERRY”
“And you can see the result: Perfectly aligned text!” the teacher surveyed the room. “Here's a little something for you. You see this LOCATE command? Well, it moves the text cursor to that location. You can simply tell the computer to move the cursor there. You can verify this by checking the CSRX and CSRY variables. Try this program:”
CLS
LOCATE 6,3
? CSRX,CSRY
“You see that the computer displays 6 and 3, corresponding to the cursor x location and cursor y location. CSRX and CSRY is what is known as built-in variables. That is, the computer maintained a specific set of variables that it automatically updates as necessary.
“CSRX and CSRY are what is known as Read-Only variables. That is, you can read them, but you cannot write to them. You can set the values by using locate command. Now, there are 2 kinds of variables: Numeric, and Text. The difference between the two is that numeric stores numbers, and text stores string, or array of characters. We'll discuss array later. Now, let's define our own variables, numbers and string. You see this X,Y, and TEXT? Let's do a program that uses these variables and sets them.
INPUT X
INPUT Y
INPUT T$
CLS
LOCATE X,Y
? T$
END
“So, when you run this program, the computer shows a question mark. That's input X. Then input Y. Then input the text. By the way, the END keyword is there to tell the computer to END the program right there. Normally, the program ends when it runs out of command/instruction. However, I want you to practice good coding technique and always ends the program with the END command.”
The students entered the text on the table and see that it works just like before, except they have to enter the number individually, every time they run the program.
“Teacher, isn't this incovenient that we have to type in the numbers every time we run the program?” asked Nancy.
“Ah, in this case, sure. Later on, I'll show you a trick regarding DATA so that you don't have to do that. For now, I want you to understand that you can have variables, and that there are two kinds of variables.”
“Yes, teacher.”
“Also, you can have more than one variables. Do you see that there is more than one entry on the table? You can put that into an array. That is, you can say X#1, X#2, X#3 and so on.
“The trick is that you need to tell the computer ahead of time how many X,Y,and T$ variables you want. You use the DIMension (DIM) command for that. In fact, let's do a real quick program right now. How many of you know MADLIBS?”
“It's the program where you insert words to a story.”
“Correct, let's write a quick MADLIBS program right now.”
CLS:CLEAR
DIM WORD$[4]
?”NAME”:INPUT WORD$[0]
?”ANIMAL”:INPUT WORD$[1]
?”ITEM”:INPUT WORD$[2]
?”COLOR”:INPUT WORD$[3]
?
? WORD$[0];” HAD A LITTLE “;WORD$[1]
? ”IT'S FLEECE AS “;WORD$[3];” AS “;WORD$[2]
“Notice several things. We specified 4 instances of WORD$ variables, and we use all four of them. Specifically, we use the ones from 0 to 3. This is because arrays start from zero. When you use other programming languages, you need to check whether the array starts from zero, or from one. You cannot assume because the one you're learning starts from zero, then all arrays starts from zero. You need to check that for every language you learn.
“Another thing is the placement of WORD$. Notice that I use WORD$[3] ahead of WORD$[2]. This is because the text uses word #3 ahead of word #2. The most important thing is to associate the index with the content. Take this table, for instance:
INDEX WORD$
0 MARY
1 LAMB
2 SNOW
3 WHITE
“You can experiment with these things around. For now, I want you to understand that whatever number you put in as the index of the array, that's what you'll get. Now run the program a few times, and see if you can get a handle of it, because for homework, you'll be doing exactly this kind of things.”
“One more thing: Don't put spaces between the variable name (WORD$) and the array index([1]). Technically, you can use parenthesis, but if you use that, it'll be confusing for when you get to complex program. So use bracket as array notation.”
Once the student finished entering the program, they ran the program. Some of them are laughing. “Look! I got 'Jack has a little goldfish and its fleece is green like mushrooms!'” The students are clearly enjoying themselves.
“Teacher!” said Nancy, “I have 'Duplicate Definition' error! What do I do?”
“Hmmm. I think that's array based error. See here? You forgot to CLEAR the memory! You need both CLS and CLEAR command.”
“Why is that?”
“CLS clears the screen. CLEAR clears the variables out of the memory. Two different things.”
Once Nancy added the CLEAR command, everything works fine.
“Alright, one more thing and we'll be done for the day. It's time for you to learn DATA command. Conceptually, it's very simple. You learned how to INPUT value into the variables. Now, you're going to learn how to do it automatically. Take a look at the table again. You see that WORD$ array has value associated with it. You can actually do this piece of code:”
@MARY
DATA “MARY”,”LAMB”,”SNOW”,”WHITE”
@JACK
DATA “JACK”,”GOLDFISH”,”MUSHROOM”,”GREEN”
@TOM
DATA “TOMMY”,”CAT”,”BELL”,”GOLDEN”
“So, tell me what you see. Do you see 3 separate data there?”
“Yes, teacher. For Mary, Jack, and Tom.”
“Good. Notice that there is an ampersand character in front of the label. That's how the computer knows that it is a label. Now, here is the completed program. I'm simply replacing the INPUT command with READ command.” Completed program is on Appendix A.
RESTORE @MARY
?”NAME”:READ WORD$[0]
?”ANIMAL”:READ WORD$[1]
?”ITEM”:READ WORD$[2]
?”COLOR”:READ WORD$[3]
“Notice that I also put the command RESTORE @label, in the beginning. This is how the program knows which DATA statement to read. You know how there's a cursor for the character text screen? CSRX, and CSRY?”
“Yes, teacher.”
“Well, there's another cursor, a hidden one, for the DATA command. Just as LOCATE moves the character text screen cursor to a certain location, the RESTORE command moves the DATA cursor to a certain location, namely, the specified label. Then whenever READ command is encountered, the computer simply read the next DATA entry. You need to make sure to read strings as string, and numbers as number, or bad things will happen.”
“If you want to use data for Tommy, then the restore command should specify TOM. Like this:
RESTORE @TOM.
“This label basically references the label that is written on the program. It doesn't matter where you put the DATA statements, but the end of the program is a good place to be.”
“Why is that, teacher?”
“The explanation for that is rather complex, so I'm going to leave it for now. Just make sure you know how RESTORE/READ/DATA commands work. You're going to be needing to use that a lot in the near future. One more thing. You see the blocks of the program? They represent Init-Input-Output-Data. Familiarize yourself with it, since it's a common construct.”

No comments:

Post a Comment