Chapter 6

Answers to Exam Preparation Exercises

1.     The WHILE loop is a looping statement, repeating the DO clause while the condition is true. The IF statement executes the THEN clause once, if the condition is true.

2.     Output from code segment:

2

3

4

5

6

7

8

9

10

11

5.     The output will be

14

13

12

11

        The first digit on each line (the one) is J and the second digit is I.

6.     Flawed code segment.

(a)    4     6     8    10    12    14    16    18    20

and so on until numeric overflow.

(b)   The two flaws are in the initial value of X and in the loop termination. X is initially set to 2, but the 2 is not printed before X is incremented to 4, which can be fixed by incrementing X after the Write statement instead. The loop never stops because X never equals 15; X always is even. This may be corrected by changing the Boolean expression in the WHILE statement to X < 15.  Corrected code:

    X := 2;

    WHILE X < 15 DO

      BEGIN

        Write(X)

        X := X + 2;

      END

7.     Code segment to copy a line from Input to Output: 

(a)    LINE is output.  The 1 is not output because EOLN becomes true when the character '1' is input and the loop terminates.

(b)   Corrected code:

    WHILE NOT EOLN DO

      BEGIN

        Read(Ch);

        Write(Ch)

      END

8.     No priming Read is necessary because use of EOLN as the condition in the inner WHILE loop guarantees that all characters are read in and that empty lines are handled properly.  The <eoln> mark is skipped over with the Readln statement.

11.   (a)    At the end of execution of Program PreTest, Sum contains 18 and Number contains 0. 

(b)   No, the given data set does not fully test the program.  An­other set needs to be tested where the value of I may reach its limit, another where the first data item is the sentinel value of zero, and yet another where all data values before the sentinel are less than zero.

12.   Invariant for

Sum := 0;

SumSquare := 0;

Count := 0;

WHILE NOT EOF DO

  BEGIN

     Readln(Number);

     Count := Count + 1;

     Sum := Sum + Number;

     SumSquare := SumSquare + Sqr(Number)

  END;

        The invariant is that EOF is False, there is another number to read in, Count contains the number of numbers that have been read in so far, Sum contains the sum of the numbers read in so far, and SumSquare contains the sum of the squares of the numbers read in so far.

14.   No, this segment will not work. The first character read is discarded, <eoln> marks are arbitrarily read across and printed (as blanks), and the last character on each line is discarded. The code should look like

WHILE NOT EOF DO

  BEGIN

    WHILE NOT EOLN DO

      BEGIN

        Read(Character);

        Write(Character)

      END;

    Readln;

    Writeln

  END;

Answers to Programming Warm-Up Exercises

3.     Nested loop to print a triangle of numbers:

Line := 1;

WHILE Line <= 4 DO

  BEGIN

    Number := 1;

    WHILE Number <= Line DO

      BEGIN

        Write(Number,' ');

        Number := Number + 1

      END;

    Writeln;

    Line := Line + 1

  END

4.     Class average:

Count := 0;

Sum := 0;

WHILE NOT EOF DO

  BEGIN

     Read(Score);

     Sum := Sum + Score;

     Count := Count + 1

  END;

IF Count > 0

  THEN

     Average := Sum / Count;

5.     (a) 

Writeln('         Sales');

Writeln('Week1    Week2    Week3');

(b) 

Writeln(Week1:5, '    '

        Week2:5, '    '

        Week3:5);

8.     Output hours and minutes:

AMPM := 'A';

AM := 1;

WHILE AM <= 2 DO      (* Loop through halves of day, starting at 1 AM *)

  BEGIN

    Hour := 1;

    WHILE Hour <= 12 DO        (* Loop through 12 hours starting at 1 *)

      BEGIN

        IF Hour = 12                      (* Switch between AM and PM *)

          THEN

            IF AMPM = 'A'

              THEN

                AMPM := 'P'

              ELSE

                AMPM := 'A';

        Minute := 0;

        WHILE Minute <= 59 DO   (* Loop though 60 mins, starting at 0 *)

          BEGIN

            IF Minute < 10 (* Special format for single-digit minutes *)

              THEN

                Writeln(Hour:2, ':0', Minute:1, ' ', AMPM, 'M')

              ELSE

                Writeln(Hour:2, ':', Minute:2, ' ', AMPM, 'M');

            Minute := Minute + 1

          END;

        Hour := Hour + 1

      END;

    AM := AM + 1      (* Start second half of day -- 1 PM to 12:59 AM *)

  END

9.     Hours in a day, take two:

Hour := 1;

TenMinute := 0;

AM := True;

Done := False;

WHILE NOT Done DO

  BEGIN

    Write(Hour:2, ': ', TenMinute:1, '0');

    IF AM

      THEN

        Write(' A.M. ')

      ELSE

        Write(' P.M. ');

    TenMinute := TenMinute + 1;

    IF TenMinute > 5

      THEN

        BEGIN

          TenMinute := 0;

          Hour := Hour + 1;

          Writeln;

          IF Hour = 13

            THEN

              Hour := 1;

          IF Hour = 12

            THEN

              AM := NOT AM;

        END

    IF (Hour = 1) AND (TenMinute = 0) AND AM

      THEN

        Done := True

  END;

11.   The following code segment processes all of the lines in a data file called Numbers, printing the count, sum and average for each input line. We need to declare one new variable, Numbers, of type Text. The extended code segment is:

Assign(Numbers, 'Numbers.dat');

Reset(Numbers);

 

WHILE NOT EOF (Numbers) DO

  BEGIN

 

    (* Code segment from question 10 goes here *)

  END;

12.   The following code segment processes all of the lines in a data file called Numbers, printing the count, sum and average for each input line. In addition it prints the count, sum and the average of all of the input values once <eof> is reached. We need to declare three new variables: Total_N of type Integer; Total_Sum of type Integer; and Total_Average of type Real. The extensions to the code segment are:

Assign(Numbers, 'Numbers.dat');

Reset(Numbers);

 

Total_N := 0;

Total_Sum := 0;

 

WHILE NOT EOF(Numbers) DO

  BEGIN

 

    (* Code segment from question 10 goes here *)

 

    (* Add count and sum of integers on line to totals *)

    Total_N := Total_N + N;                                 (* Count *)

    Total_Sum := Total_Sum + Sum;

 

  END;

 

Total_Average := Total_Sum / Total_N;

Writeln('Number of integers in the file: ', Total_N:1);

Writeln('Sum of the integers in the file: ', Total_Sum:1);

Writeln('Average of the integers: ', Total_Average:1);