Exercise 4.2
- For each fragment, state the number of times that the body
of the for statement will be performed.
-
for decreasing count : 20 .. -20
.
.
-
var bound : int := -100
for decreasing index : bound .. bound div 4
.
.
-
var a : int := 3
for decreasing lcv : a * 5 .. a div 5
.
.
for decreasing i : 1 .. 10
.
.
for decreasing i : 100 .. 50 by 5
.
.
- The program shown has a number of errors. Correct them and then determine
the output of the corrected program.
var x : real
for x := 10 .. 1
put This is step ..
endfor
put x
- Trace the program shown below with input of 763
const width : int := 5
var divisor, quotient, remainder : int
divisor := 1
for position : 1 .. width -1
divisor := divisor * 10
end for
get remainder
for decreasing position : width .. 1
quotient := remainder div divisor
put quotient ..
remainder := remainder mod divisor
divisor := divisor div 10
end for
put ""
- Assuming the the variables x and y have been declared
as type int, write for statements that will print values
of the function y = 2x + 5 for the indicated values of x.
- 6, 5, 4, ..., 0
- -7, -5, -3, ..., 7
- 0, 3, 6, ..., 30
|
- 50, 45, 40, ..., 5
- -15, -10, -5, ..., 10
- 40, 36, 32, ..., 8
|
|
|