What Does This Program Do?
Worksheets: WDTPD Worksheet #1, WDTPD
Worksheet #2, WDTPD Worksheet #3, WDTPD
Worksheet #4,
WDTPD Worksheet #5
Every programmer in every language must be able to accurately deskcheck program code. There are times when it is necessary to carefully follow the execution of computer code without actually using the computer or programming language software. At other times, it is necessary to follow someone else's computer code that may or may not be adequately documented. This ACSL topic requires the student to follow programs and program segments and accurately predict the outcome. A command of the language that you are using is required but all successful programmers must also understand the general structured flow of execution that is used by all compilers (BASIC, C, C++, Java, Pascal, etc.)
For this ACSL topic, we will be using the TRS-80 dialect of BASIC. This language is similar enough to MS Visual Basic that a decent Visual Basic programmer could understand it. However, there are a number of functions and statements that do not exactly work in Visual Basic, therefore you must study the following examples in order to be successful in this ACSL topic. Being familiar with different dialects of BASIC will make you a better computer programmer.
You must be familiar with the following relevant topics:
the evaluation of arithmetic expressions, single & nested loops (FOR & DO loops), branching (IF/THEN statements), subscripted variables, multi-dimensional arrays, and string manipulations.
I. Functions
A. INT - The INT (stands for Integer) function returns the next least whole number. In other words, it truncates a number down. This is especially tricky when the argument (the number in the parentheses) is negative. Look at the examples closely.
1. Examples
a/ INT(3.2) = 3
b/ INT(-5.4) = -6
c/ INT(0) = 0
d/ INT(-0.4) = -1
2. Exercises
B. ABS - The ABS (stands for Absolute Value) function returns the absolute value of the argument.
1. Examples
a/ ABS(-3) = 3
b/ ABS(0) = 0
c/ ABS(14) = 14
d/ ABS(1236) = 1236
e/ ABS(-23.45) = 23.45
f/ ABS(5 * -4) = ABS(-20) = 20
2. Exercises
C. LEFT$(A$, N) - The LEFT$ (pronounced, "left string") function returns the first N characters (including embedded spaces) from the left end of the string, A$. It requires two arguments. If the value for N is 0, the empty (or null) string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned (with no extra padded spaces).
1. Examples
a/ LEFT$("Programming", 3) = "Pro"
b/ LEFT$("Basic", 1) = "B"
c/ LEFT$("Wyomissing Area", 11) = "Wyomissing "
Note that here is a trailing space in the
answer after the 'g'
d/ LEFT$("abcd", 0) = ""
(the null string)
e/ LEFT$("maryland", 3 - 2) = "m"
You must evaluate the expression, 3-2, before
using LEFT$
f/ LEFT$("high school", 100) = "high school"
2. Exercises
a/ LEFT$("baloney",
3)
b/ LEFT$("John
Wayne", 6)
D. RIGHT$(A$, N) - The RIGHT$ (pronounced, "right string") function returns the last N characters (including embedded spaces) from the right end of the string, A$. It requires two arguments. If the value for N is 0, the empty string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned. The characters that are returned are not reversed, but rather they keep their original relative order.
1. Examples
a/
RIGHT$("Programming", 3) = "ing"
b/
RIGHT$("where", 10) = "where"
c/ RIGHT$("United
States", 7) = " States" Note
there is a leading space in the answer before the 'S'
d/
RIGHT$("a", 1) = "a"
e/ RIGHT$("abc
", 1) = " " A blank space
2. Exercises
a/ RIGHT$("baloney", 3)
b/ RIGHT$("abc def", 5)
E. MID$(A$, B, C) - The MID$ (pronounced, "mid string") function returns the next C characters from the string A$, starting with (and including) the Bth character. It requires three arguments. If the value for C is 0, the empty string is returned. If the value for C exceeds the number of remaining characters after the Bth character, the remaining portion of the string is returned nevertheless with no extra padded spaces.
1. Examples
a/
MID$("computer", 4, 2) = "pu"
b/
MID$("computer", 1, 3) = "com"
c/ MID$("Wyo
Area", 3, 4) = "o Ar" Note that the blank space is counted as a
character.
d/
MID$("Wyomissing", 4, 1) = "m"
e/ MID$("high
school", 6, 0) = "" the empty string
f/
MID$("abcdef", 5, 24) = "ef"
2. Exercises
a/ MID$("baloney", 3, 2)
b/ MID$("Wyo Area Hi", 6, 19)
F. LEN - The LEN (stands for Length) function returns the length of a string. You obtain the length of a string simply by counting the number of characters (including spaces) in that string. The length of the null string is zero.
1. Examples
a/
LEN("computer") = 8
b/ LEN("a") =
1
c/ LEN("Wyo
Area") = 8 The answer is NOT 7, because the blank space counts as a
character.
2. Exercises
a/ LEN("mustard")
b/ LEN("peanut butter and jelly")
G. STR$ - The STR$ (stands for String and pronounced "string function") function turns a value into a string literal. The final answers to all of the examples below must include double quotes.
1. Examples
a/ STR$(13) =
"13"
b/ STR$(2.789) =
"2.789"
c/ STR$(0) =
"0"
d/ STR$( 4 + 8) =
"12"
2. Exercises
a/ STR$(3)
b/ STR$(76.53)
H. VAL - The VAL (stands for Value) function turns a string literal (that happens to "look like" a numerical value) into a numerical value that could later be added, subtracted, multiplied, etc. in a mathematical expression. Until a string literal such as "15" is turned into a value (like 15, without the quotes), it cannot be manipulated mathematically.
1. Examples
a/
VAL("22") = 22
b/
VAL("-8.9") = -8.9
c/ VAL("b15")
= syntax error since "b15" cannot be turned into a numerical value simply by
"stripping off the quotes."
2. Exercises
a/ VAL("25.6")
b/ VAL("0.0002")
I. SQR - The SQR (stands for Square Root) function returns the square root of the argument.
1. Examples
a/ SQR(9) = 3
b/ SQR(25) = 5
c/ SQR(7) = 2.64575
2. Exercises
J. SGN - The SGN (stands for Sign) function returns the sign of the argument. That is, it returns -1, if the argument is a negative number. It returns 1 if the argument is a positive number and it returns 0 if the argument is 0, itself. The SGN function returns no other values than 0, 1, and -1.
1. Examples
a/ SGN(13) = 1
b/ SGN(-23) = -1
c/ SGN(4.345) = 1
d/ SGN(76-324) = -1
e/ SGN(0) = 0
2. Exercises
a/ SGN(-23.4567)
b/ SGN(5
- 3 * 2)
II. Statements - You must also know how to evaluate several key BASIC statements that control the flow and that determine the logic of a program.
A. INPUT statement - One commonly used statement in BASIC is the INPUT statement. Presenting a variable name after the keyword INPUT stops the program and allows the user to enter a value for that variable. For example,
DIM A AS INTEGER
INPUT A
PRINT A
END
The user is given the chance to input a value for A. That value is then printed on the screen by the PRINT statement.
DIM A, B AS INTEGER
INPUT A, B
IF A > 4 THEN PRINT "A"
IF B > 10 THEN PRINT B
END
Note that two variables are declared in the program above.. Two values must be inputted by the user when the program executes. The order in which the user inputs the 2 values will determine which one is stored in A and which value is stored in B. The IF/THEN statements are one-line IF/THEN's. In the first IF/THEN, if the condition is TRUE then the string literal "A" is printed because of the double quotes around the A. In the second IF/THEN, if the condition is TRUE then the value for the variable B is printed on the screen.
DIM A AS INTEGER
INPUT A
IF A > 35 THEN
PRINT (A + 2)
ELSE
PRINT (A - 2)
END IF
END
In the program immediately above the IF/THEN/ELSE statement could be replaced by a one-line version as in:
DIM A AS INTEGER
INPUT A
IF A > 35 THEN PRINT (A + 2) ELSE PRINT (A - 2)
END
B. FOR Loop - The FOR loop in BASIC is an example of a repetition statement. It allows a set of statements to continually be executed over and over again for a specific number of iterations. A loop variable is used after the keyword FOR and "lower" and "upper" bounds are specified. On the last line of the loop the keyward NEXT is followed by the loop variable. By default, the loop variable increments by one each time the loop iterates. When the loop variable equals the upper bound it iterates one more time. Then, the loop variable increments but the loop does not iterate and the rest of the program (below/after the loop) executes.
The numbers 1 through 5 are printed based on the code in the following program. The loop variable is A. Its final value, by the way is 6, not 5, because it is incremented one last time. Note that statements within the body of a loop should be indented for increased readability.
DIM A AS INTEGER
FOR A = 1 TO 5
PRINT A
NEXT A
END
Can you predict what prints out based on the execution of the following example?
DIM A, B AS INTEGER
B = 5
FOR A = 1 TO 10
IF A + B >= 12 THEN PRINT "GREATER"
NEXT A
END
ACSL Resources | Basic Programming | Computer Science Using C++