V22.0002.002 - SPRING 2000, Midterm 1

1. (25 points) Write a program that translates an 8-digit binary number to it's decimal equivalent.

Sample run:

```   Enter base 2 number: 10110011
Decimal equivalent : 179
```

## Algorithm

1. Set the variable result to 0.
2. Multiply result by 2.
4. Repeat steps 2 and 3 eight times.
5. Display result.
Hints, hints, hints:
• Since you are reading one digit at a time, you must read the digit as a character, then evaluate it's integer equivalent.

```   PROGRAM Binary;

VAR result, index : integer;
digit          : char;

BEGIN
write('Enter base 2 number: ');
result := 0;

{ Read 8 digits, one at a time, and compute result }

FOR index := 1 TO 8 DO
BEGIN
result := result * 2 + ord(digit) - ord('0');
END;
writeln('Decimal equivalent : ', result);
END.
```

Extra credit (15 points) What would you do to modify the previous program to handle numbers in any base 2 through 9 ? Your new version must ask the user for the base of the number.

Sample run:

```   Base of number     : 4
Enter base 4 number: 30321012
Decimal equivalent : 52806
```

1. Create an integer variable base.
3. In step ii of the algorithm, instead of multiplying by 2, multiply by base
4. Declare result as longint to handle bases greater than 4.

2. (25 points) Superman read of Zorro's program to produce Zs and became jealous: he wanted a similar program to produce his Ss and you have drawn that assignment. The program should ask for the width of the letter and then display an S that is roughly the same height as it's width (depending if the width is even or odd, the height might be one greater than the width). Sample run:

```   Enter width: 8
SSSSSSSS
S
S
S
SSSSSSSS
S
S
S
SSSSSSSS

```
Hints, hints, hints:
• You will have 5 distinct loops
1. Top horizontal line
2. First vertical line
3. Middle horizontal line
4. Second vertical line
5. Bottom horizontal line
• Each of the two vertical segments will have (width DIV 2 - 1) characters.

```   PROGRAM Superman;

VAR width, index : integer;

BEGIN
write('Enter width: ');

{ Top horizontal line }

FOR index := 1 TO width DO
BEGIN
write('S');
END;
writeln;

{ First Vertical line }

FOR index := 1 TO (width DIV 2 - 1) DO
BEGIN
writeln('S');
END;

{ Middle horizontal line }

FOR index := 1 TO width DO
BEGIN
write('S');
END;
writeln;

{ Second vertical line }

FOR index := 1 TO (width DIV 2 - 1) DO
BEGIN
writeln('S':width);
END;

{ Bottom horizontal line }

FOR index := 1 TO width DO
BEGIN
write('S');
END;
writeln;

END.
```

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

```   PROGRAM Test;
VAR ind1, ind2 :  integer;
BEGIN
FOR ind1 := 0 TO 3 DO
FOR ind2 := ind1 TO 4 DO
write(chr(ord('a')+ind2));
writeln;
END.
```

```   abcdebcdecdede
```

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

``` 1. PROGRAM Test;
2. VAR index : integer;
3.    digit : char;
4. CONST limit : 5;
5. BEGIN
7.    FOR index := 1 TO limit DO
8.    BEGIN
10.       digit := digit + index;
11.	  write(digit:4:2);
12. END.
```
1. Line: 4__    Error: Initialize CONST with "=", not ":"
2. Line: 6__    Error: Argument of read cannot be CONST, must be a variable
3. Line: 10_    Error: + operator cannot be used with character
4. Line: 11_    Error: Precision qualifier can be used only with reals, not characters
5. Line: 12_    Error: END missing at the end of the FOR loop

5. (15 points)

1. What is the value of the following expression ?
(9 DIV 2) + (9 MOD pred(4))       4__

2. How many times does the following loop execute ?
FOR I := 1 DOWNTO (5 DIV 5) DO       1__

3. How many times does the following loop execute ?
FOR I := 'a' TO succ(pred('d')) DO       4__

4. Given the following input:
```   4 Valerio 63
42.1 a
```
What will be the value of r after the following statements ?       42.1_

```   VAR n    : string[7];
c, b : char;
r    : real;
a    : integer;
...
```
5. Given the following input:
```   53 3 2
b
4
```
What will be the value of i2 after the following statements ?       3__

```   VAR c1, c2 : char;
i1, i2 : integer;
...