-
[10 points]
Problems #3 and #4 of Chap. #6 Exercises 6.2 on pg. 249 of the textbook.
3. A := 2;
FOR LCV := (3 * 2 - 4) TO 10 * A DO
writeln("**", LCV:4);
** 2
** 3
** 4
** 5
** 6
** 7
** 8
** 9
** 10
** 11
** 12
** 13
** 14
** 15
** 16
** 17
** 18
** 19
** 20
- (#4)
4. FOR LCV := 50 DOWNTO 30 DO
writeln(51 - LCV:5);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-
[10 points]
Problem #17 of Chap. #6 Exercises 6.2 on pg. 251 of the textbook.
- [Answer]
- (#17)
| Original |
Solution |
Sum := 0;
FOR Index := 1 TO 4 DO
BEGIN
writeln("*":21 + Index);
Sum := Sum + Index;
END;
*
*
*
*
|
Sum := 0;
Count := 1;
FOR Index := 4 DOWNTO 1 DO
BEGIN
writeln("*":21 + Count);
Sum := Sum + Index;
Count := Count + 1;
END;
*
*
*
*
|
-
[20 points]
Problems #10 and #11 of Chap. #6 Exercises 6.3 on pg. 265 of the textbook.
10. A := 2;
WHILE A <> 20 DO
BEGIN
writeln(A);
A := A * 2
END;
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
-32768
.
.
.
0
{ This is an imfinite loop because A will never equal 20
therefore, the condition will also be true (i.e. A <> 20) }
- (#11)
11. B := 15;
WHILE B DIV 3 = 5 DO
BEGIN
writeln(B, B DIV 5);
B := B - 1
END;
153
{ Not an infinite loop!!! }
-
[20 points]
Problem #18 of Chap. #6 Exercises 6.3 on pg. 266 of the textbook.
Ch is X eoln is FALSE eof is FALSE
Ch is Y eoln is FALSE eof is FALSE
Ch is Z eoln is FALSE eof is FALSE
Ch is eoln is FALSE eof is FALSE
Ch is 1 eoln is FALSE eof is FALSE
Ch is 3 eoln is TRUE eof is FALSE
Ch is
eoln is FALSE eof is FALSE
Ch is A eoln is FALSE eof is FALSE
Ch is B eoln is FALSE eof is FALSE
Ch is C eoln is FALSE eof is FALSE
Ch is eoln is FALSE eof is FALSE
Ch is 2 eoln is FALSE eof is FALSE
Ch is 1 eoln is TRUE eof is FALSE
Ch is
eoln is FALSE eof is FALSE
Ch is M eoln is FALSE eof is FALSE
Ch is N eoln is FALSE eof is FALSE
Ch is O eoln is FALSE eof is FALSE
Ch is eoln is FALSE eof is FALSE
Ch is 2 eoln is FALSE eof is FALSE
Ch is 5 eoln is TRUE eof is FALSE
Ch is
eoln is TRUE eof is FALSE
Ch is
eoln is TRUE eof is TRUE
-
[20 points]
Problems #20, #23 and #26 of Chap. #6 Exercises 2.3 on pg. 267 of the textbook.
Sum := 1;
WHILE Sum > 0 DO
BEGIN
write('Input 3 integer values [E.g. 1 2 3]? ');
readln(Num1, Num2, Num3);
Sum := Num1 + Num2 + Num3;
IF Sum > 0 THEN
writeln('Sum = ', Num1, ' + ', Num2, ' + ', Num3, ' = ', Sum)
END;
- (#23)
Count := 1;
PrevSquare := 1;
Difference := 0;
WHILE Difference < 25 DO
BEGIN
CurrSquare := sqr(Count);
Difference := CurrSquare - PrevSquare;
IF Difference <= 25 THEN
write(CurrSquare:6);
IF Count MOD 5 = 0 THEN
writeln;
Count := Count + 1;
PrevSquare := CurrSquare
END;
{ Output from code above}
1 4 9 16 25
36 49 64 81 100
121 144 169
- (#26)
Count := 1;
write('Input a positive real num [E.g. 3.5]? ');
readln(PositiveRealNum);
WHILE PositiveRealNum >= 0 DO
BEGIN
IF PositiveRealNum >= 0 THEN
write(PositiveRealNum:7:2);
IF Count MOD 10 = 0 THEN
writeln;
PositiveRealNum := PositiveRealNum - 0.5;
Count := Count + 1;
END;
-
[20 points]
Problems #15 and #16 of Chap. #6 Exercises 6.7 on pg. 294 of the textbook.
PROGRAM chap6pg294prob15 (input, output);
CONST
MAXCOLUMN = 5;
MAXROW = 5;
VAR
Column : integer;
Row : integer;
BEGIN
writeln;
FOR Row := 2 TO MAXROW DO
BEGIN
FOR Column := 1 TO MAXCOLUMN DO
write(Column*Row:6);
writeln;
END;
writeln;
END.
- (#16)
4 5 6 7
4 5 6 7
4 5 6 7
4 5 6 7
5 6 7
5 6 7
5 6 7
6 7
6 7