- Consider the following program segment:
sum := 0;
FOR count := 0 TO 4 DO
sum := sum + count;
WRITELN (sum)
What will be printed to the screen when this segment is executed?
10
Because there is no BEGIN-END pair, only the assignment statement is inside the
FOR-DO loop. The WRITELN is outside of the loop, and is only executed once.
- Consider the following program segment:
FOR ch := 'a' TO 'd' DO
FOR i := 1 TO 2 DO
WRITE (ch)
What will be printed to the screen when this segment is executed?
aabbccdd
- What is the output produced by the following code?
count := 1;
total := 0;
REPEAT
count := count + 2;
total := total * count;
WRITE (total)
UNTIL count > 10
00000
- Determine the screen output of the program shown below.
PROGRAM test2; Output
VAR 1020
start lo1020
x, y : start hi1020
INTEGER; leave hi1119
leave lo1119
PROCEDURE hi ( c, d : INTEGER ); 1119
BEGIN { hi }
WRITELN ( 'start hi', c, d ); Note that the parameters in lo
c := c + 1; are VAR, but not in hi
d := d - 1;
WRITELN ( 'leave hi', c, d )
END; { hi }
PROCEDURE lo ( VAR a, b : INTEGER) ;
BEGIN { lo }
WRITELN ( 'start lo', a, b );
hi ( a, b ); {changes made by hi are NOT seen by lo}
a := a + 1;
b := b - 1;
WRITELN ( 'leave lo', a, b )
END; { lo }
BEGIN { test2 }
x := 10;
y := 20;
WRITELN ( x, y );
lo ( x, y ); {changes made by lo ARE seen by main}
WRITELN ( x, y )
END. { test2 }
- Write a complete Pascal program that asks the user to enter a
Value, and then displays on the screen the integer values from 0 to
ROUND(Value), inclusive. Repeat this process until the user enters a
value of 0. You should not worry about comments, prologues, etc.
PROGRAM lister;
VAR
Value : REAL;
Step : INTEGER;
BEGIN
WRITELN ('Please enter a numeric value; enter 0 to exit: ');
READLN (Value);
WHILE Value <> 0 DO
BEGIN
WRITE ('Your list of numbers is: ');
FOR Step := 1 TO ROUND(Value) DO
WRITE (Step, ' ');
WRITELN; {ends the line with the list of numbers}
WRITELN; {prints a blank line to separate the next list}
WRITELN ('Please enter a numeric value; enter 0 to exit: ');
READLN (Value)
END {While}
END.
- Consider the following code segment:
REPEAT
WRITE ('Enter a non-negative number: ');
READLN (value);
IF value < 0
THEN
WRITELN ('invalid input - please try again')
UNTIL value >= 0
Re-write this code segment using a WHILE loop.
WRITE ('Enter a non-negative number: ');
READLN (value);
WHILE value < 0 DO
BEGIN
WRITELN ('invalid input - please try again');
WRITE ('Enter a non-negative number: ');
READLN (value);
END
- A Pascal programmer noticed that the program segment
FOR num := 1 TO 10 DO
WRITELN (num);
next_num := num + 1;
WRITELN (next_num)
produced different results on different machines. Explain why.
The variable num is the FOR-DO loop control variable. The value
of this variable is unreliable outside of the loop (because of the
different ways different machines implement these loops) until it is
assigned a new value. Thus the third line uses an unreliable value
in calculating next_num, which is then printed.
- Why is it considered bad style to use variable parameters in functions?
By design, a function is intended to return a single value through its
own function name. In fact, the name of a function is usually a noun
to reflect this single value. If other values are also returned through
variable parameters, these changed values might easily be overlooked.
Notable exception: array parameters are often passed as variable parameters
(to save stack space) even when they are not going to be changed.
- What are the major differences and similarities between WHILE-DO and REPEAT-UNTIL loops?
WHILE-DO loops are pre-tested; therefore, since the test can initially be
false, it is possible for such a loop to execute zero times.
REPEAT-UNTIL loops are post-tested, and so mst execute at least once.
Both loops are "indeterminate," meaning that the total number of times they will
execute is not known in advance.
- We want to be able to square numbers N times n a row. The Nth square of a number
(number^(2^N)) can be produced by the code
Result := Number;
FOR Count := 1 TO N DO
Result := Result * Result;
Nth_Square := Result;
Write a procedure MultiSqr (Number, N, NSqr) that would calculate the Nth square of
Number and return the answer in NSqr. (All values are integer);
PROCEDURE MultiSqr ( Number, N : INTEGER;
VAR NSqr : INTEGER );
VAR Count, Result : INTEGER;
BEGIN
Result := Number;
FOR Count := 1 TO N DO
Result := Result * Result;
NSqr := Result
END;
- Write a function NthSqr (Number, N) that calculates the Nth square of Number.
FUNCTION NthSqr (Number, N : INTEGER) : INTEGER;
VAR Count, Result : INTEGER;
BEGIN
Result := Number;
FOR Count := 1 TO N DO
Result := Result * Result;
NthSqr := Result
END;