Chapter 4

Answers to Exam Preparation Exercises

1.     The main advantage of having a program input its data rather than writing all of the values as constants is that the program does not have to be changed when a new set of data needs to be run through it. If all values were in the CONST section, those values would have to be modified and the program recompiled every time it was run. When the program inputs the values, it can run on many different data sets without modification of the source code.

3.     An <eoln> is entered from the keyboard by pressing the carriage return key. A program can generate an <eoln> in its output by executing a Writeln statement. This will cause a carriage return character and a line-feed character to be output, so that the cursor moves to the next line.

4.     In Turbo Pascal, a non-printing control character is input when Read attempts to input <eoln> into a Char variable. In Standard Pascal, when a Read statement encounters <eoln>, it will input a blank into a variable of type Char.

5.     False. Real values may not be read into Integer variables. It will work the other way around -- Integer values may be read into Real variables.

7.     A = 14,  B = 19,  C = 67,  D = 73

9.     Reading values.

(a) R = 11,  S = 12.35,  Ch1 = ' ',  Ch2 = 'A'

(b) Ch1 = '1',  R = 1,  S = 12.35

10.   Input prompt refers to a message printed out directly before a Read statement, telling the user what kind of input is expected by the Read statement. Echo printing refers to printing out the values of variables directly after they are read, in order to verify that the variables contain the proper values.

13.   The benefits of using top-down structured design are that the code produced using this method will be readable, understandable, easily debugged, and easily modified.

15.   Number2 contains the integer value 2; the position at which it was determined that the string '-#701' is not a valid number.

Answers to Programming Warm-Up Exercises

2.     Changing the answer to exercise 1 to Readln(Length, Height, Width); has no effect on the values read into the variables.  The difference between the two statements is that after the first is executed the marker position is on the <eoln> mark and after the second is executed the marker is on the first position of the following input line.

5. Code segment to read in data:

VAR

  Blank, Chl, Ch2, Ch3, Ch4, Ch5, Ch6, Ch7:

    Char;

  Nl, N2, N3, N4, N5, N6:

    Integer;

  Rl, R2, R3:

     Real;

 

Readln(Chl, Nl, Rl, Blank, Ch2, N2);

Readln(R2, Blank, Ch3, Blank, Ch4, R3, N3);

Readln(Ch5, N4, Blank, Ch6, N5, Blank, Ch7, N6);

6.     Assuming a String variable called Name has been defined, the following statements read and print the three names from the file to the screen.

Assign(Infile, 'NAMES.DAT');

Reset(Infile);

Readln(Infile, Name);

Writeln(Name);

Readln(Infile, Name);

Writeln(Name);

Readln(Infile, Name);

Writeln(C1, Name);

Close(InFile)

7.  Interactive segment for novice user:

Writeln('Enter your age');

Readln(Age);

Writeln('Enter your height');

Readln(Height);

Writeln('Enter your weight');

Readln(Weight);

Writeln('Enter the initial of your first name'); Readln(First);

Writeln('Enter the initial of your last name'); Readln(Last);

        Interactive segment for experienced user:

Writeln('Enter age, height, and weight');

Readln(Age, Height, Weight);

Writeln('Enter initials');

Readln(First, Last);

10.   Algorithm for entering and running a program. This will vary from system to system. The example here is for Turbo Pascal Version 6.0 on a PC with a hard disk:

 Main module.

   Turn system on.

   Enter date and time.

   Start the Turbo system.

   Enter program.

   Run program.

 

Start the Turbo System.

   Type "turbo" and press return.

 

Enter program.

   Press F3.

   Enter the name of your program (with .PAS at the end)

     in the dialog box, and press return.

   Type the program into the editor.

   Use the cursor and backspace keys to make corrections.

 

Run program.

   Press F2 to save the file.

   Press Ctrl-F9 to run the program.

   If an error message appears, note what it says,

      and then press any key. Otherwise, enter data as

      needed and observe any output, until the program

      stops running.

   Press Alt-X to exit the Turbo system.

11.   Algorithm for quadratic formula.

Main Module

  Open files

  Read three coefficients from input file

  Calculate two Real solutions

  Write solutions to output file

 

Open files

  Open file InQuad for input

  Open file OutQuad for output

 

Read three coefficients from file InQuad

  Read coefficient A

  Read coefficient B

  Read coefficient C

 

Calculate two Real solutions

  First solution  S1 = (-B + Sqrt(Sqr(B) - 4*A*C))/(2*A)

  Second solution S2 = (-B - Sqrt(Sqr(B) - 4*A*C))/(2*A)

 

Write solutions to file

  Write S1 to file OutQuad

  Write S2 to file OutQuad

12.   Assume that S and Num_S have been declared as type String; Comma and E_P have been declared as type Integer; and Number and Num_I have been declared as type LongInt. Note that for the right hand side of an assignment statement to be treated as a LongInt, at least one of the operands must be of type LongInt, therefore, variable Num_I is declared as LongInt.

Readln(S);                         (* Read the string         *)

Comma := Pos(',',S);               (* Find position of comma  *)

Num_S := Copy(S,1,Comma-1);        (* Copy string up to comma *)

Val(Num_S, Num_I, E_P);            (* Convert to integer      *)

Number := Num_I * 1000;            (* Multiply by 1000, store *)

Num_S := Copy(S,Comma+1,Length(S));       (* from after comma *)

Val(Num_S, Num_I, E_P);            (* Convert to integer      *)

Number := Number + Num_I;          (* Add to the number       *)