Chapter 3

Answers to Exam Preparation Exercises

3.     Evaluating.expressions

(a) 3  (b) 4  (c) 37  (d) 22  (e) 23  (f) 100

4.     A = 5 and B = 2 (Assumption:  Writing an integer value with no fieldwidth specified defaults to a width of 1). 

Statement         Produces

 

Writeln('A = ', A:6, ' B = ', B:6);   A = 5 B = 2

Writeln('Sum = ', A + B:6);           Sum = 7

Writeln(A DIV B:6);                   2

Writeln(B - A:6);                     -3

6.     Write the assignment statement in algebraic notation.

7.     Evaluate the expressions.

(a)  0.23529  (b)  2.25  (c)  44.2  (d)  5  (e)  0  (f)  21 (g)  2  (h)  5  (i)  1

8.     Show the program's output. Underscore indicates blank:

AB

___413_is the value of X

2.1800000000E+01_is the value of Y

9.     Evaluate the expressions:

(a) 9.1  (b) 7.0  (c) 27  (d) 6  (e) 5.0

10.   a) 26 b) 'reparation' c) 1 d) 16 e) 16 f) 16.

12.   The output from the statements (• indicates blank):

(a) •47810.59322

(b) 47810.5932

(c) ••47810.5932200

(d) 47810.6

(e) 0.031

(f) •••-0.031067

(g) -3.1E-02        (Equivalent to Y:7:-3)

Answers to Programming Warm-Up Exercises

3.     This is a laboratory exercise for the student and requires no answer here.

4.     This also is a laboratory exercise for the student, but it requires that changes be made to the program before it is entered.  A sample is given here.

PROGRAM Two (Output);

 

(* Programming Assignment Two                            *)

(* William T. Verts                                      *)

(* June 27, 1986                                         *)

(* This program computes the cost per ounce given total  *)

(* cost and the weight expressed in pounds and ounces.   *)

 

CONST

  TCost = 600;    (* Total cost                   *)

  Pounds = 10;    (* Weight in pounds             *)

  Ounces = 11;    (* Additional weight in ounces  *)

 

VAR

  TotOz,   (* Total weight in ounces  *)

  UCost:   (* Computed cost per ounce *)

     Real;

 

BEGIN

  TotOz := 16 * Pounds;

  TotOz := TotOz + Ounces;

  UCost := TCost / TotOz;

  Writeln('Cost per unit ', UCost)

END.

5.     Complete the program:

PROGRAM Rectangle (Output);

 

(* This program finds the perimeter and the area of a rectangle *)

(* given the length and width                                   *)

 

VAR

  Length,              (* Length of the rectangle    *)

  Width,               (* Width of the rectangle     *)

  Perimeter,           (* Perimeter of the rectangle *)

  Area:                (* Area of the rectangle      *)

    Real;

BEGIN (* Rectangle *)

  Length := 10;                     (* Assigns a value to Length *)

  Width := 5;                       (* Assigns a value to Width  *)

  Perimeter := Length * 2.0 + Width * 2.0; (* Computes perimeter *)

  Area := Length * Width;                       (* Computes area *)

  Write('The perimeter of a rectangle with length ',

        Length:8:2, ' and width ', Width:8:2, ' is');

  Writeln(Perimeter:8:2);

  Writeln('and the area of the rectangle is ', Area:8:2)

END.  (* Rectangle *)

6.     Assignment statement:

Sum := N * (N + 1) DIV 2

8.     Pascal fragment to output substring positions:

First := Pos('res', Sentence);

RestOfSentence := Copy(Sentence, First+3, Length(Sentence));

Position := Pos('res', RestOfSentence);

Second := First + 2 + Position;

RestOfSentence:=

  Copy(RestOfSentence, Position+3, Length(RestofSentence));

Position := Pos('res', RestOfSentence);

Third := Second + 2 + Position;

Writeln('Second Occurence of res: ', Second, ' Third: ',Third);

9.     Change exercise 8 to work with user-specified string.

First := Pos(Target, Sentence);

RestOfSentence := Copy(Sentence, First+3, Length(Sentence));

Position := Pos(Target, RestOfSentence);

Second := First + 2 + Position;

RestOfSentence :=

  Copy(RestOfSentence, Position+3, Length(RestofSentence));

Position := Pos(Target, RestOfSentence);

Third := Second + 2 + Position;

Writeln('Second Occurence of ', Target, ': ', Second,

  ' Third: ', Third);