Answers to Self Checks, Extended Prelude to Programming

Extended Prelude to Programming
Third Edition
Answers to Even-numbered Review Exercises
Chapter 0
2.
ENIAC
20.
d
4.
minicomputer
22.
a
6.
Web pages
24.
a
8.
motherboard
26.
b
10.
1024 or 102
28.
c
12.
DVD
30.
False
14.
laser
32.
False
16.
operating system
34.
True
18.
FORTRAN
36.
False
20.
b
38.
c, b, a, d
Chapter 1
2.
program development cycle
4.
data
6.
Weight
8.
declared
10.
character
12.
b
14.
True
16.
True
18.
True
20.
(a) 4
(b) 100
22.
(a) 4
(b) 0
24.
(a) 234,000
(b) 0.0000234
26.
Write “Enter your age: “
Input Age
28.
Write “Enter your age: “
Input Age
Set New = Age – 5
Write “You don’t look a day over “, New
30.
1.25 or 1
32.
No
34.
(a) none
(b) Set Number1 = 4
Set Number1 = Number 1 + 1
Set Number2 = 3
Set Number 2 = Number1 * Number2
(c) Write Number 2
36.
Write “Enter a number: ”
Write “Enter another number: “
38.
Write “The result of the computation is: “
40.
Declare Number1 As Integer
Declare Number2 As Integer
Chapter 2
2.
output
4.
call
6.
pseudocode
8.
step
10.
logic
12.
design documentation or trade study documentation
14.
dialog box
16.
object-oriented programming
18.
b
20.
c
22.
c
24.
c
26.
a
28.
True
30.
False
32.
False
34.
False
36.
True
38.
False
40.
True
42.
True
44.
Answers may vary. Some attributes include height, width, color, lock type
Some methods include opening, closing, locking
46..
48.
Input Data Module
Write “Enter three numbers: “
Input Number1, Number2, Number3
Calculate Average Module
Set Average = (Number1 + Number2 + Number3) / 3
Output Results Module
Write “The average of the numbers ”
Write “Number1, “, “ , Number2, “and “ Number3
Write “is “, Average
50.
Answers will vary. Some possible sets of test data are:
0, 0, 0 or 2, 6, 985 or -253.45, 987, 0 etc.
Chapter 3
2.
If-Then-Else
4.
d
6.
(a) False
(b) False
(c) True
(d) False
8.
(a) True
(b) False
(c) False
(d) False
10.
False
12.
Bye
14.
Write “Enter a number: “
Input NUM
If Num = 1 Then
Write “Yes”
End If
16.
18.
Input Num
If Num = 1 Then
Write “Yes”
End If
If Num <> 1 Then
Write “No”
End If
20.
= , <> , < , <= , > , >=
22.
(a) N <= 0 (b) (N < 0) OR (N > 5)
24.
(a)
(b)
Input Num
If Num = 1
Write
End If
If Num = 2
Write
End If
If Num = 3
Write
End If
Input Num
Then
“Yes”
Then
“No”
Then
“Maybe”
If Num = 1 Then
Write “Yes”
Else
If Num = 2 Then
Write “No”
Else
If Num = 3 Then
Write “Maybe”
End If
End If
End If
(c)
26.
Input Num
Select Case of Num
Case 1:
Write “Yes”
Case 2:
Write “No”
Case 3:
Write “Maybe”
End Case
multiple alternative structure
28.
Input X
If X > 6 Then
Write “Pass”
Else
If (X = 5) OR (X = 6) Then
Write “Retest”
Else
Write “Fail”
End If
End If
30.
5
Not
And
32.
(a)
34.
Change the line as follows: If Grade = “A” Then
36.
YES
1
Done
(b)
2
Done
(c)
3
Done
38.
the Else clause
40.
No
Chapter 4
2
greater than
4.
validate
6.
(a) True
(b) True
8.
(a) False
(b) True
10.
True
12.
False
14.
False
16.
False
18.
2
1
20.
2
1
(c) True
22.
24.
26.
3
5
7
Write “Enter a negative number: “
Input NegativeNumber
While NegativeNumber >= 0
Write “Enter a negative number: “
Input NegativeNumber
End While
1
-1
28.
(a)
30.
Write “Enter an integer: “
Input MyInteger
While MyInteger <> Int(MyInteger)
Write “Enter an integer: “
Input MyInteger
End While
32.
16
(b)
34.
36.
Set A = 0
Set Total = 0
For B = 1 Step 1 To N
Set A = A + 2 * B – 1
Set Total = Total + (2 * B – 1)
Set B = B + 1
End For
Write A
Set Average = Total / N
38.
4
5
8
10
12
15
40.
Hello
Chapter 5
2.
3
4.
6
6.
True
8.
False
10.
False
12.
False
14.
True
16.
Declare Numbers[25]
Write “Enter a number. Enter 0 when done.”
Input MyNumber
Set K = 0
While MyNumber <> 0
Set Numbers[K] = MyNumber
Set K = K + 1
End While
18.
4
1
9
20.
Correct the While statement as follows:
While (Found = 0) AND (Index < N)
Also, change Set Found = 0 inside the If-Then clause to:
Set Found = 1
22.
A[K] = 20,
24.
Change the If-Then statement to:
If A[K] > A[K + 1] Then
and change the Set Flag = 1 inside the If-Then clause to:
Set Flag = 0
26.
Write Name[0], Name[9]
28.
Write Length(FullName)
30.
Set First = FullName[0]
For K = 1 Step 1 To 24
If FullName[K] = “ “ Then
Set Last = FullName[K + 1]
End If
End For
Write First, “.” , Last. “.”
A[K + 1] = 10
32.
Declare Score1[25], Score2[25], Score3[25] As Real
34.
For K = 0 Step 1 To 24
Write Score1[K], Score2[K], Score3[K]
End For
36.
For K = 0 Step 1 To 4
Set Sum = 0
For J = 0 Step 1 To 4
Set Sum = Sum + X[K, J]
End For(J)
Write Sum
End For(K)
Dewey4
Dewey5
38.
Chapter 6
2.
fields
4.
binary
6.
direct access or random access
8.
control variable
10.
True
12.
False
14.
True
16.
True
18.
True
20.
False
22.
Open “people” For Input As NameFile
While NOT EOF(NameFile)
Read NameFile, PersonName
Write PersonName
End While
Close NameFile
24.
“A”,25<CR>”C”,20<CR>”E”,15<CR><EOF>
26.
“D”,90<CR><EOF>
28.
(a) Character, String
30.
5
(b) Integer, Real
32.
Shirley and Tamara
34.
“Tamara”<CR>”Shirley”<CR>”Sam”<CR>”Michael”<CR>”Marjorie”<
CR>”Corinne”<CR><EOF>
Chapter 7
2.
exported
4.
IPO chart
6.
global
8.
built-in
10.
7
12.
True
14.
2 * X ( i.e., 2)
X (i.e., 1)
5
16.
Display:
Input
 assigned to Num1
 assigned to Num2
 assigned to Num3
Processing
Num1, Num2, Num3
2
18.
1
None
5
20.
Subprogram Flip(X As Refm Y As Ref)
Declare Temp As Real
Set Temp = X
Set X = Y
Set Y = Temp
End Subprogram
22.
local:
global:
X in Sub, Num1, Num2
X in main, Y
24.
5
1
4
26.
5
1
2
28.
(a) 4
30.
(a) “-1.5”
32.
(b) 1
2
(b) 87.6 ,(N=1)
Output
Num1, Num2, Num3
34.
36.
3
Main
Declare X, Y As Real
Write “Enter a number: “
Input X
Write “Enter another number: “
Input Y
Write Average(X, Y)
End Program
38.
three times
40.
Function Fac(N) As Integer
Set Product = 1
For K = 2 Step 1 To N
Set Product = Product * K
End For
Set Fac = Product
End Function
42.
Function Mult(M, N) As Integer
Set Total = 0
For K = 1 Step 1 To N
Set Total = Total + M
End For
Set Mult = Total
End Function
Chapter 8
2.
object
4.
Public
6.
inheritance
8.
parent (or base), child (or derived)
10.
d
12.
c
14.
b
16.
True
18.
True
20.
False
22.
False
24.
False
26.
False
28.
SetSide, ComputeArea, GetArea, GetSide
30.
Call Square1.SetSide(20)
32.
Write “The area is “ Square1.GetArea
34.
SetSide, GetArea, GetSide, SetHeight, GetHeight,
ComputeArea
36.
(a) Text box:
(b) Label objects:
38.
Date
40.
name, caption or text, enabled
42.
Subprogram OKbutton.Click()
If Option1.Value = True Then
Call Window1.Open
Else
Call Window2.Open
End If
End Subprogram
44.
Subprogram EnterButton.Click()
Set Sum = Val(NumberBox.Text)
Set NumberBox.Text = “ “
End Subprogram
Hello
The message is:
Chapter 9
2.
pseudorandom numbers
4.
Car.Make, Car.Model
6.
indexed
8.
(a) True
10.
True
12.
True
14.
True
16.
True
18.
False
20.
True
(b) True
22.
(a) Random(6)
24.
For K = 1 Step 1 To 50
Write Random(6)
End For
26.
(a) 33 = “!”
(b) 65 = “A”
28.
(a)
(b)
30.
Index = 2
32.
1
34.
5
36.
Smith
38.
25 bytes (characters)
40.
SeekGet DataFile, 1, Car
Write Car.Make, Car.Model
42.
Set Last = LOF(DataFile) / Length(Car)
SeekPut DataFile, Last + 1, Car
44.
SeekGet DataFile, 10, Car
Set Car.Model = “Explorer”
SeekPut DataFile, 10, Car
46.
Set Count = 0
Set Found = 0
While (NOT EOF(IndexFile)) AND (Found = 0)
Read IndexFile, Model
Set Count = Count + 1
If Model = “Camry” Then
Set Car.Make = “ “
Set Car.Model = “ “
SeekPut DataFile, Count, Car
Set Found = 1
End If
End While
“30”
(b) Random(2) – 1
(c) 126 = “~”
“-21.5”