Sample run:

How many scores ? 7 Enter 7 scores: 32 77 80 55 19 95 66

32 77 80 55 19 95 66

- The input and output will be handled in the same
`FOR`loop. - You have to take advantage of the
*input buffer*. - The column number is given by an
*integer operation*on the scores. -
**Pay attention to the end points of each range: 20, 40, 60, 80 and 100**

PROGRAM Test; { Arrange scores in 5 columns } USES Crt; VAR score, howMany, index, column : integer; BEGIN ClrScr; write('How many scores ? '); readln(howMany); write('Enter ', howMany,' scores: '); { Loop through scores } FOR index := 1 TO howMany DO BEGIN { Read one score at a time and process } read(score); { To get the column: a) subtract 1 to score (for end points) } { b) DIV by 20 and add 1} column := (score - 1) DIV 20 + 1; writeln(score:(column*4)); END; END.

2. (25 points) We want to produce the 16 bit binary representation of an unsigned integer in the range 0-65535. To obtain the binary representation, in reverse order, you repeatedly do the following operations:

- If the number is
*odd*, output`1`, otherwise output`0`. - Divide the number by
`2`, taking only it's integer part.

Sample run:

Enter number: 77 Reversed binary: 1011001000000000

- Since we want the
*16 bit*representation, the program will have to execute the loop 16 times.

PROGRAM Binary; { Output 16 bit binary version of number in reverse order } USES Crt; VAR number : word; { number unsigned 16 bit integer } index : integer; BEGIN ClrScr; write('Enter number: '); readln(number); write('Reverse binary: '); { Loop through the 16 bits } FOR index := 1 TO 16 DO BEGIN { Display 1 for odd, 0 for even } write(number MOD 2); { Update number } number := number DIV 2; END; writeln; END.

3. (20 points) What does the following code produce ?

PROGRAM Test; VAR ind1, ind2, ind3 : integer; BEGIN FOR ind1 := 1 TO 4 DO FOR ind2 := 1 TO 4 DO BEGIN FOR ind3 := 1 TO (ind1 * ind2) DO write('+'); END; END.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Those are 100 (10+20+30+40) '`+`'.

4. (15 points) The following code contains ** at least 5** syntax
errors. Identify the line number and the error.

1. PROGRAM Test; 2. VAR value, limit : real; 3. index, test : integer; 4. BEGIN 5. value := 50; 6. readln('limit'); 7. FOR index := 1 TO limit DO 8. BEGIN 9. test := index * value; 10. writeln(test:4:1); 11. END; 12. END.

Number | Line | Error |

1. | 3 |
Cannot use same name (test) for program and for variable. |

2. | 6 |
Cannot use a constant string as argument to readln. |

3. | 7 |
limit is a real and cannot be used as the end
value of a FOR loop. |

4. | 9 |
The operation index * value returns a real
and test is an integer variable. |

5. | 10 |
test is an integer variable, the precision
qualifier in writeln can be used only with
reals. |

5. (15 Points)

i What is the value of the following expression ? 4.0(12 - 3) DIV (17 MOD 6) * (8 / 2)ii How many times does the following loop execute ? 11FOR I := 10 DOWNTO (33 MOD 11) DOiii How many times does the following loop execute ? 4FOR I := 'a' TO CHR(ORD('b')+2) DOiv Given the following input: 4 Valerio 63 42.1 aWhat will be the value of rafter the following satements ?2.1 VAR n : string[20]; c, b : char; r : real; a : integer; ... readln(a, n); read(c, r, b);v Given the following input: 53.0 3 2 b 4What will be the value of c1after the following satements ?b VAR c1, c2 : char; r : real; i1, i2 : integer; ... read(r); read(i1); readln(i2); readln(c1); readln(c2);