` COMPUTE! ISSUE 52 / SEPTEMBER 1984 / PAGE 138`

 PROGRAMMING THE TI C. Regena Writing An Educational Program I'm sure you already know or have read what a "good" educational program should contain. I'd like to discuss how you actually program an educational program. I decided that the best way I could describe the process was to write a program, then provide a step-by-step explanation of what I did. The hardest part of writing any program is deciding the topic and the type of program—drill and practice, tutorial, simulation, game, etc. I picked a very popular topic for computer programs, the Morse code, and decided to do a drill-and-practice program. Quite a few readers have requested programs for secondary school students, so next month I'll present a tutorial on a high school subject. Memorization Quiz A drill-and-practice program is useful for any subject that requires memorization. The usual procedure is to ask a question, then have the student input an answer. If you can avoid INPUT and use CALL KEY instead, there will be much less chance for errors or "crashing" the program. In the "Morse Code" program, the quiz will be to press the letter or number after the computer displays a code. I decided to use the numbers from 0 to 9 and the whole alphabet in the quiz. Since each number and letter corresponds to a code, I set up the array M\$ to contain the codes. M\$(0) through M\$(9) will hold the codes for the numbers in order from 0 through 9. The alphabet will be in M\$(10) through M\$(35). Since we need 36 elements for the array, line 160 dimensions M\$. Lines 170-190 READ the codes for M\$ from data in lines 200-250. The data items are in order—first the numbers then the alphabet—each item separated by a comma. Dots And Dashes I started out using periods for dots and minus signs for dashes, but decided it was too difficult to type periods with commas—too much chance for typing errors in the DATA statements. Also, the minus sign requires the SHIFT key and the period doesn't, so the typing was a little more complex. I looked on the ASCII character code chart to see what symbols I wouldn't be using in regular printing and decided to use the ampersand (&) to represent a dash and the percent sign (%) to represent a dot. I borrowed my son's Morse code chart and converted the dots and dashes into % and & signs. These codes are in the DATA statements of lines 200-250. You may use longer DATA statements if you like (the TI accepts up to four screen lines for each numbered line), but I kept the statements shorter to make it a little easier to type and debug. The next step was to design the graphics—the dots and dashes. The % sign represents a dot in the DATA statement codes and is redefined in line 140 using a CALL CHAR statement so that it will draw a dot on the screen. The & sign is redefined as a bar-shaped figure in line 150. When a dash is printed on the screen, it will actually be three & signs placed together. The subroutine in lines 360-470 is the main section of coding that translates a code in M\$ to the graphic representation on the screen. Looking at a code, if the symbol is % we need to draw a dot, and if the symbol is & we need to draw a dash. This process continues for the entire data, which can be from one to five dots and dashes. Line 360 instructs the computer to check from 1 to the length of the data (which will be from 1 to 5). Line 370 assigns a one-character value to A\$ for every increment of the FOR-NEXT loop in line 360. This one-character value is the symbol in the Jth place of the string in the DATA statements. Lines 380-430 instruct the computer to print a dot if the symbol is % and a dash (which is &&&) if the symbol is &. I put a space after the dot or dash to separate them slightly on the screen. You could use CALL HCHAR instead if you wish, but I used PRINT. By printing with semicolons, everything will stay on the same line and be printed right after the previous printing. Making Some Noise Since the TI has sound, we can use sound in our Morse code program. Besides that, real Morse code transmission is by sounds. Line 390 plays a sound for a dash, and line 420 plays a different sound for a dot. I used a sound duration of 300 for the dash and 60 for the dot. As you learn the Morse code, you'll probably want to shorten those durations. You should also try different frequencies instead of the one I chose (131) or combinations of frequencies and noise numbers to get a sound you like. Line 440 stops the sound so that dots and dashes are distinct. If you don't have this statement, dashes would run together and you wouldn't be able to tell how many dashes there should be. Line 450 forces the loop to go to the next symbol in the code. Line 460 PRINTs to get off the present line (colon means "go to the next line" in printing) and add an extra line between codes. Line 470 returns program execution from this subroutine. Returning To The Menu I thought it would be nice to review the numbers and letters before having to take the quiz, so there are three sections: Numbers, Alphabet, and Press a Key. Numbers will print each number and show the corresponding Morse code. Alphabet will go through the whole alphabet in order and print each letter with its code. In Press a Key the student can press any number or letter, and the computer will print the code. In any of these sections the student can at any time press ENTER, and the demonstration will stop and the program will return to the main menu screen. The procedure to see the codes for the numbers is in lines 560-670. Line 570 begins the FOR-NEXT loop with the counter I varying from 0 to 9 for the numbers. The number is printed (by printing I), then the subroutine at 360 is called which deciphers the code M\$(I) into the dots and dashes and prints the code on the screen while playing the tones. Line 600 calls subroutine 480, which is simply a delay loop to create a slight pause between numbers. Lines 520-530 check to see if the student has pressed ENTER to return to the main menu screen and stop the numbers section. The Alphabet section, lines 680-790 is similar to the Numbers section. This time the loop counter I varies from 10 to 35, and the codes will go in order from M\$(10) to M\$(35), which are the letters from A to Z. To print the letters with the codes, line 700 uses the CHR\$ function. The ASCII codes of the letters are from 65 to 90. Since the loop counter I varies from 10 to 35, the ASCII codes for CHR\$ are 55 + I. In the Press a Key section, the student may press a letter or number and the computer will display the code. This section could be used as a quick review for students who want to study certain letters. The student may also spell words and phrases one letter at a time to see and hear the Morse code equivalent. Lines 840-920 detect which key is pressed. If the ENTER key (K = 13) is pressed, the program branches back to the main menu screen. The IF-THEN statements make sure that only a number or a letter is pressed; all other keys are ignored. The variable K holds the ASCII value of the key pressed, and lines 900 and 930 relate K to the variable I which is used to print the code M\$(I). The instructions are in lines 970-1040, and the quiz is contained in lines 1050-1490. The quiz consists of all ten numbers and 26 letters. An array N() is set up so each of the 36 elements from 0 to 35 is equal to 1. This is in lines 1050-1070. Later as one of the numbers or letters is answered correctly, N(I) will be set to zero so it cannot be chosen again. Line 1080 initializes the number of guesses G to zero for the scoring. The quiz loop first chooses a random number (I) from 0 to 35 (line 1140). If the number has previously been answered correctly, N(I) will be zero and another number I is chosen. Lines 1160-1190 determine the correct answer L for the number I, which will be the ASCII code of the number or letter chosen. Line 1200 calls the subroutine to print and sound out the code chosen, and line 1210 increments the number of guesses. Lines 1220-1290 detect the key the student presses; makes sure it is ENTER, a number, or a letter; and then prints the key pressed. If the key pressed is ENTER, the program branches back to the main menu and the quiz ends. Lines 1300-1390 determine if the key pressed is the correct answer. If the answer is incorrect, an "uh-oh" sound is played and the program branches back to line 1200 to display and sound the code again and wait for another answer. If the answer is correct, an arpeggio is played. After the code is answered correctly, line 1400 sets N(I) to zero so that code cannot be chosen again, and line 1410 goes to the next problem. The student must get the right answer to continue the quiz. Quiz Variations You can change the program to give the right answer if the student misses. Instead of lines 1330 and 1340, print CHR\$(L) or CALL HCHAR or CALL VCHAR and put L on the screen, then branch to line 1400. In this case you might want to keep a score of number correct and number incorrect. You might want to allow that missed letter or number to be shown again. Branch to line 1410 instead of 1400, and before you branch set Z = Z - 1. Another way would be to GOTO 1140 instead of changing the loop counter Z and going to the NEXT Z. If you prefer to let the student guess two or three times before the correct answer is given, set up a flag (FLAG = 0) at line 1155 then at line 1340 increment the flag (FLAG = FLAG + 1). You could then branch, depending on the value of FLAG, either back for another guess or to give the answer and branch to the next problem. You might prefer to have a quiz of a certain number of codes, say 10, rather than all 10 numbers and 26 letters. Change line 1130 to FOR Z=l TO 10. Using lines 1150 and 1400 will still prevent the quiz from choosing the same number or letter more than once. Another idea would be to have an infinite quiz. Take off the FOR-NEXT loop, lines 1130 and 1410. Also, you won't need lines 1150 and 1400 (and 1050-1070) because the numbers and letters can keep being chosen. Now the quiz keeps going until the student presses ENTER to return to the main menu screen. In this type of quiz you may want to make sure the code is not the same as the previous one. We can use a variable PI for previous I chosen, and add these two lines: ```1150 IF PI = I THEN 1140 1155 PI = I ``` You can change the Numbers and Alphabet sections to fit your needs also. To change the delay time between codes, change the upper limit in line 480. Instead of 200, put your own number; a larger number will be a longer delay. Instead of using a delay between numbers and letters, you can have the student press any key to continue, or press the appropriate number or letter. You can change the following lines: ```650 IF K<> I + 48 THEN 610 655 NEXT I 770 IF K<> I + 55 THEN 730 775 NEXT I ``` The program is flexible enough that you can change it to do exactly what you want it to do. You can even change the graphics and make it a quiz to learn Braille, or sign language, or some other type of code. You can use words instead of the alphabet and make a quiz for reviewing a foreign language, or perhaps vocabulary words. Structuring Your Programs A couple of readers have suggested that I include flowcharts with my programs. My secret is that I haven't touched a flowchart since it was required in my college FORTRAN class years ago. In answer to your questions of how I plan a program, I just sit down at the computer and start typing. With this program, I got to line 350 and typed ```350 ON K-4B GOTO 1000, 2000, 3000,4000, 5000 ``` then worked on a section at a time, not necessarily in order. The Numbers section started with line 1000, Alphabet with line 2000, Press a Key with line 3000, the quiz with line 4000, and 5000 was END. As I realized I needed subroutines, I numbered them 400, 600, and 700, making sure I didn't get to line 1000. On the TI it doesn't really matter where you put the subroutines; you can put them all at the end if you prefer. Anyway, after everything was running properly and each section was tested, I used the RES command to get all the line numbers to look nice. Each programmer has his or her own way of planning, and there's really no right way or wrong way. I say if it works, you're successful. If you wish to save typing effort, you may obtain a copy of Morse Code by sending \$3, a blank cassette or disk, and a stamped, self-addressed mailer to: C. RegenaP.O. Box 1502Cedar City, UT 84720 Be sure to specify the title and that you need the TI version.