Kathleen M. Huhner
(figure available in print form)
(figure available in print form)
10 READ L, W
(figure available in print form)
20 LET A = L * W
(figure available in print form)
30 PRINT “AREA =”;A
(figure available in print form)
40 GO TO 10
(figure available in print form)
50 DATA 33,5,7,9,2.8,1.1
(figure available in print form)
60 END
Data to be used: 33,5, 7, 9, 2.8, 1.1
Until now, the teacher and students have been working together. The teacher has given assignments, has discussed them with the students, and has given them a solid understanding of flowcharting and eight commands in BASIC. A test should be given. It should test the students’ knowledge of flowcharting, error detection, and programming abilities. (Appendix B contains a sample test for this purpose.) The students should be graded on their logic and programming efficiency. As soon as the tests are returned, the flowcharts should be discussed with the class.
Students should now feel comfortable with the ideas of flowcharting and programming. They should be able to analyze and construct a program from a flowchart (and vice versa), and they should be able to write and process a computer program utilizing everything contained in the schedule weeks two through six (part II).
At this point, students are ready for an introduction to loops. Looping is an important and powerful technique. As such, they deserve careful study. The diagram below provides the essential components of any loop.
(figure available in print form)
These components may occur in a different order (except the initialization), but the, must all be present in a loop, Also, note that EXIT does not necessarily imply STOP—when a loop is completed, the computer exits to the first step (after the loop) that can be executed, To make sense out of the diagram on the preceding page, look at the following programs (each component is identified as it occurs),
Write a program to print the squares of 4,6,8, and 10.
10 LET X=4
|
Initialization
|
20 PRINT “THE SQUARE OF”;X;”IS”;XI2
|
Body
|
30 LET X=X+2
|
Incrementation
|
40 IF X > 10 THEN 60
|
Stopping Mechanism
|
50 GO TO 20
|
Re-entry of loop
|
60 END
|
Exit
|
Write a program to read five test scores and print the average of these scores.
10 LET Z=0
20 LET I=1
|
Initialization
|
30 READ T
|
Body
|
40 LET Z=Z+T
|
Body
|
50 IF I=5 THEN 80
|
Stopping Mechanism
|
60 LET I=I+1
|
Incrementation
|
70 GO TO 30
|
Re-entry of loop
|
80 PRINT “THE AVERAGE IS”;Z/5
|
Exit
|
90 DATA 97,85,63,76,81
100 END
Since an iterative process is common, computer languages simplify it. The students should now learn a pair of commands to accomplish the same tasks as the programs above—the FOR/NEXT loop.
The FOR statement contains the index, the initial value, the increment value, and the final (or test) value, The NEXT statement contains the increment step, the test step, and the re-entry step. For example, the second program above can be compressed into:
10 LET Z-O
20 FOR I=1 TO 5
30 READ T
40 LET Z=Z+T
50 NEXT I
The rest of the program remains the same.
It should be stressed that the FOR/NEXT loop does not apply to indefinite loops (e.g. to count a sequence of numbers of unknown length)—it is used only when you know how many times to repeat a loop.
A test should be given to the students. A few of my favorite problems follow.
-
1. Given any three numbers, in any order, print the numbers in decreasing order.
-
2. Manhattan Island was purchased in 1626 for $24. If those early buyers had invested the same amount in certificates of deposit (at 17 1/2% interest), how much would their investment be worth today?
-
3. As shipping clerk you are responsible for shipping quantities of products as cheaply as possible. It is generally less expensive to send 50 items in one large box than 25 in each of 2 boxes. Read in the following data:
-
____
A. Stock number (SN)
-
____
B. Number of items to be shipped (IS)
-
____
C. Maximum number of items that can be contained in
-
____
____
I. the largest box (LB)
-
____
____
II. the middle-sized box (MB )
-
____
____
III. the smallest box (SB)
-
____
____
It is understood that LB MB SB, and that there may be only one or two sizes of boxes.
-
____
____
For each set of data, write a line containing
-
A. Stock number
-
8. N1 = the number of boxes of LB
-
C. N2 = the number of boxes of MB
-
D. N3 = the number of boxes of SB
-
____
____
Your program should place as many of the items as possible in the largest number of the large boxes, then place as many {or any remaining) items in the smallest box in which they will fit.
I have found that students should make a copy of their programs. Then, they take their copies, debug their programs, and get them to run. In this manner, the students become aware of certain errors to avoid.
A natural result of discussion of the FOR/NEXT is the discussion of subscripted variables. Subscripted variables have a wide variety of programming applications. For instance, a student wishes to sort a list of numbers. Before entering a list of numbers, a DIM statement must be used to set aside space in the memory for each list to be used. For example,
10 DIM X(100)
Secondly, the student must read in the amount of numbers in the array (not to exceed 100 for this example). This can be accomplished as follows:
20 READ N
30 FOR I=1 TO N
40 READ X(I)
50 NEXT I
60 DATA .,. . . .. . . ..
Thirdly, a nested loop (a loop within a loop) is necessary in order to perform the sort. The partial program below shows how to sort an array into descending order.
80 REM THE OUTER LOOP
BEGINS
90 FOR I=1 TO N-1
100 REM THE INNER LOOP
BEGINS
110 FOR J=I+1 TO N
120 IF X(I) =X(J) THEN 160
130 LET Z=X(J)
140 LET X(J)=X(I)
150 LET X(I)=Z
160 NEXT J
17O REM THE INNER LOOP ENDS
180 NEXT I
190 REM THE OUTER LOOP ENDS
Lines 120 through 140 pull a “switch.” These lines are a swapping mechanism—a dummy location (Z) is needed in order to keep the value of X(J). (Without the “dummy” X(J)=X(I) at the end of the loop.)
Locating the real roots of a polynomial are handled in programming by means of subscripted variables, where we think of a polynomial as an array containing the coefficients in ascending powers of X. Here are some examples:
POLYNOMIAL
|
ARRAY
|
5x
2
+ 6x
4
Ð3x
5
|
0 0 5 0
|
6
|
-3
|
94x
|
0 94
|
5-6x
3
+ 4x
|
|
5 4 0 -6
|
Notice in the first and third examples that 0’s are used in the array for missing powers of the variable. An assignment should be given to the students once they feel comfortable with the idea of subscripted variables—two of my pet problems follow.
-
1. The Fibonacci sequence begins 1,1,2,3,5,8,13, . . ., where each term from the third on is the sum of the two preceding terms.
-
____
(a) Given A and B, print all terms of the Fibonacci sequence that lie between A and B.
-
____
(b) Find N such that the sum of the first N terms of the Fibonacci sequence exceeds 10 7.
-
2. (a) Write a program that creates a file which contains a list of N student numbers and test scores. There are five scores for each student. The computer should input student numbers and scores until “9999” is typed as the student number.
|
(b) Write a program that uses the file of (a) to type out a table with each student’s number and his average score for the five tests.
|
|
|
(c) Write a program that uses the data of (a) to type out the number of the student who received the highest score on each test and the number of the student who received the lowest score (Remember the number 9999 tells the computer to stop looking. ).
|
Thus, the introduction to BASIC ends.
Unfortunately, I haven’t the space to complete a curriculum guide for my entire programming course. I would like to mention that the second and third marking periods are concerned with Advanced BASIC. Students are taught the following: matrix manipulation, setting up, storing, and using files, statistics, plotting (on the CRT and line printer), and game and business simulations.
The fourth marking period is spent on the final project. This consists of any game which the student wishes to program. For examples of what my students have become capable of doing by this time, I shall mention some of their projects. One student wrote an impressive chess program for two players (try to make an illegal move and see what happens!); another wrote a “connect the dots” program (don’t try to cheat on this one—your lines will disappear and you’ll see spots before your eyes!); another wrote a Bingo game (complete with “blackout”); still another wrote a dog racing game (complete with bets and dogs running across the CRT). I think though, by far, the most impressive program was written by one of my freshmen—an adventure game (hopefully you will make it through and locate the hidden treasure).
All of the projects were programmed in BASIC and all of the students (except two) had little or no knowledge of programming when they began the course. I have incorporated all of the material, which I have presented, for the past two years and have met with success.