Sample Midterm #2 A22.0002

** Do any 5 problems and do all the work on the exam sheet**

1. (20 points points) What does the following program produce as output?

PROGRAM one(input, output); CONST Blank = ' '; VAR J, K:integer; BEGIN FOR j:= 1 TO 5 DO BEGIN write('*':j); FOR k:= 1 to (6 - j) DO BEGIN write(blank) END; writeln('*') END END.

*_____* _*____* __*___* ___*__* ____*_* Note: _ means blank space.

2. (20 points) Write a program that reads a number ` N` (an ` integer`)
from the keyboard and:

Prints all the numbers less than or equal to ` N`
that are prime numbers. A prime number is one that is divisible only by 1 and
the number itself. When a number is divisible by another number
there is no remainder. For example, 7 is a prime number because it is not
divisible by 2, 3, 4, 5, 6.

Hint: Test whether the values of the loop control variable, ` J`
are divisors of ` N`.
Here are some questions that should help you
do the problem:

Is ` N` read before or in the loop?

If ` j` is the loop control
variable, what is its upper limit?

What is the test determining whether ` j` is a divisor
of ` N`?

** Answer**:

PROGRAM PrimeNumbers; { Determine the prime numbers less than a given number } PROCEDURE IsPrime(number : integer; VAR answer : boolean ) { Determine if a number is prime } VAR testValue, limit : integer; BEGIN { Initialize the answer } answer := TRUE; { Test for remainders for all numbers <= sqrt(number) } { No sense testing for values greater than that since } { they cannot possibly divide the number. } { It would also be correct, but less efficient, to set } { the limit to number - 1 } limit := trunc(sqrt(number)); { Altern.: limit := number-1; } { Of course, we don't test 1 } testValue := 2; WHILE (testValue <= limit) AND (answer = TRUE) DO BEGIN IF (number MOD testValue) = 0 THEN answer := FALSE ELSE testValue := testValue + 1; END; END; { IsPrime } PROCEDURE PrintPrimes; { Get a number and print the primes that are less than it } VAR number, index : integer; answer : boolean; BEGIN write('Please enter value: '); readln(number); writeln; write('Prime numbers <= ',number,': '); FOR index := 1 TO number DO BEGIN IsPrime(index,answer); IF answer = TRUE THEN write(index,' '); END; writeln; END; { PrintPrimes } BEGIN {MAIN} PrintPrimes; END.

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

PROGRAM two(input, output); VAR J:integer; letter:char; PROCEDURE plot(J:integer); VAR I:integer; BEGIN FOR I:= 1 TO J DO write('*':i); writeln END {plot}; BEGIN FOR letter:= 'a' TO 'e' DO BEGIN J:= ord(letter) - ord('a'); plot(j) END END.

* *_* *_*__* *_*__*___* Note: _ means blank space. Note: first line is empty

4. (20 points) Evaluate the following (you may use the table on the board):

(a) (3 <= 7) AND NOT (9 = 8) (b) NOT (4 >= 5) OR (3 <= 3) (c) NOT ((5 <= 9) OR (3 < 2)) (d) (6 <= 8) OR (3 > 5) OR (3 = 3)

(a) TRUE (b) TRUE (c) FALSE (d) TRUE

5. (20 points) What does the following procedure produce for ` plot(1,'x')`?

PROCEDURE Plot( top : Integer; ch : char); VAR j : Integer; BEGIN CASE top OF 1 : FOR j := 1 TO 5 DO BEGIN writeln(ch:j, ch:(11 - (2 * j))) END; 2 : FOR j := 5 DOWNTO 1 DO BEGIN writeln(ch:j, ch:(11 - (2 * j))) END; END; END;

x________x _x______x __x____x ___x__x ____xx Note: _ means blank space.

6. (20 points) Write a procedure called ` MakeX` that produces
the block letter X by using procedure ` plot` shown in problem 5.
The block letter should be formed from asterisks (*).

** Answer**:

PROCEDURE MakeX; BEGIN { Write top half of X } Plot(1, '*'); { Write bottom half of X } Plot(2 '*'); END;