Try to Classify Variables Yourself: Answers


1.

    program squares (input, output);

    (* Print two squares *)

    var distance : integer; (* Distance between squares *)

    begin

        write('Enter the distance between squares: '); readln(distance);
        writeln;

        writeln('***', ' ' : distance, '***');
        writeln('* *', ' ' : distance, '* *');
        writeln('***', ' ' : distance, '***')

    end.

distance fixed value


2.

    program time (input, output);

    (* Transform time to seconds *)

    type
        hour = 0..23; 
        minute = 0..59
        second = 0..59;

    var hours : hour;   (* Hour of the time   *)
        mins : minute;  (* Minute of the time *)
        secs : second;  (* Second of the time *)

    begin

        write('Enter time (hour min sec , e.g.,  13 30 5): ');
        readln(hours, mins, secs);

        writeln ('At ', hours : 2, '.', mins : 2, '.',
          secs : 2, secs + (60 * (mins + 60 * hours)) : 6,
          ' seconds has elapsed.')

    end.

hours fixed value
mins fixed value
secs fixed value


3.

    program multiplication (input, output);

    (* Print a multiplication table *)

    var size,         (* Size of table *)
        row,          (* Row index     *)
        col: integer; (* Column index  *)

    begin

        write('Enter size: '); readln(size);

        writeln (' ' : 15, 'Multiplication table ');
        write (' ' : 4);
        for col := 1 to 10 do write(col : 4);
        writeln;

        for row := 1 to size do begin
            write(row : 4);
            for col := 1 to 10 do write(row*col : 4);
            writeln
        end

    end.

size fixed value
row stepper
col stepper


4.

    program occur (input, output);

    (* Count occurences of a value in an array *)

    const
        last = 7;

    type
        ArrayType = array [1..last] of integer;

    var
        value: ArrayType;  (* Array of values *)
        i,                 (* Array index *)
        key,               (* Value to search for *)
        count: integer;    (* Count of occurences *)

    begin

        writeln('Enter ', last, ' values: ');
        for i := 1 to last do read(value[i]);
        readln;

        write('Enter value to search for: ');
        readln(key);

        count := 0;
        for i := 1 to last do
            if value[i] = key then count := count + 1;

        writeln('There are ', count, ' occurences of ',
                key, ' in the array');

    end.

value fixed value
i stepper
key fixed value
count stepper


5.

    program square (input, output); 

    (* Drawing a square *)

    const maxSide = 78;      (* Max length for sides             *)      

    var character : char;    (* Character to be used for drawing *)
        side,                (* Length of sides                  *)
        i, j      : integer; (* Counters for side lengths        *)

    begin

        write('Enter character for drawing: '); readln(character);

        write('Enter side length          : '); readln(side);
        while (side < 1) or (side > maxSide) do begin
            writeln('Length incorrect. Re-enter: '); readln(side)
        end;

        writeln;

        for i := 1 to side do begin
            for j := 1 to side do write(character);
            writeln 
        end     

    end.    

character fixed value
side most-recent holder
i stepper
j stepper


6.

    program histogram (input, output);

    (* Draw a histogram *)

    const longest = 40;  (* Longest bar *)

    var amount : array [1..12] of real; (* Data for drawing     *)
        max : real;                     (* Maximum data element *)
        month,                          (* Current month        *)
        i : integer;

    begin

        for month := 1 to 12 do begin
            write('Enter amount for month ', month : 2, ': ');
            readln(amount[month]);
            if i = 1 then max := amount[1]
            else if max < amount[month] then max := amount[month]
        end;
        writeln;

        for month := 1 to 12 do begin
            write(month : 2, ': ');
            for i := 1 to round(amount[month] / max * longest) 
                do write('*');
            writeln
        end

    end.

amount fixed value
max most-wanted holder
month stepper
i stepper


7.

    program diceGame (input, output);

    (* Simulate a dice game. *)

    var die1, die2,              (* Values of dice thrown          *)
        total1, total2: integer; (* Sums of values of each player  *)
        firstPlayer: boolean;    (* First player's turn ?          *)

    begin

        Randomize;
        total1 := 0;
        total2 := 0;
        firstPlayer := True;

        while (total1 < 100) and (total2 < 100) do begin
            die1 := Random(6) + 1;
            die2 := Random(6) + 1;
            if firstPlayer then write('First') else write('Second');
            writeln(' player throws: ', die1, die2);

            if firstPlayer then
                total1 := total1 + die1 + die2
            else
                total2 := total2 + die1 + die2;

            firstPlayer := not firstPlayer;
        end;

        write('Player ');
        if total1 > total2 then write('First') else write('Second');
        write(' player won the game.')

    end.

die1 most-recent holder
die2 most-recent holder
total1 gatherer
total2 gatherer
firstPlayer stepper


8.

    program closest (input, output);

    (* Find the closest number *)

    var original,           (* Closest to this one  *)
        data,               (* Current input data   *)
        closest: integer;   (* Closest found so far *)

    begin

        write('Enter any number: '); readln(original);

        write('Enter a positive number (negative to end): ');
        readln(data);
        closest := data;

        while data >= 0 do begin
            if abs(data-original) < abs(closest-original)
                then closest := data;
            write('Enter a positive number (negative to end): ');
            readln(data);
        end;

        if closest < 0
           then writeln('No positive value entered.')
           else writeln('The closest to ', original, ' was ', closest)
    end.

original fixed value
data most-recent holder
closest most-wanted holder


9.

    program growth (input, output);

    (* Growth of capital on a bank account *)

    var capital,            (* Capital on the bank account *)
        percent,            (* Interest rate               *)
        factor,             (* Factor for yearly growth    *)
        interest: real;     (* Interest in current year    *)
        years,              (* Time to consider            *)
        i       : integer;  (* Year counter                *)
        inputOk : Boolean;  (* Is input valid ?            *)

    begin

        write('Enter capital (positive or negative): '); readln(capital);
        inputOk := false;
        while not inputOk do begin
            write('Enter interest rate (%): '); readln(percent);
            write('Enter time (years) : '); readln(years);
            inputOk := (percent > 0) and (years > 0);
            if not inputOk then begin
                writeln;
                writeln('Invalid data. Re-enter.')
            end
        end;
        writeln;

        factor := percent / 100;

        for i := 1 to years do begin
            interest := capital * factor;
            capital := capital + interest;
            writeln('After ', i : 2, 'years: interest is ',
                     interest : 11 : 2,
                     ' and total capital is ', capital : 12 : 2)
        end
    end.

capital gatherer
percent most-recent holder
factor fixed value
interest temporary
years most-recent holder
i stepper
inputOk one-way flag


10.

    program saw (input, output);

    (*  Read a sequence of values and check if they form a 'saw':
        adjacent values go up and then down. *)

    const
        last = 7;

    type
        ArrayType = array [1..last] of integer;

    var value: ArrayType;   (* Values to be checked           *)
        i:      integer;    (* Index of array                 *)
        up,                 (* Current direction is up ?      *)
        Ok: Boolean;        (* Does saw property still hold ? *)

    begin

      writeln('Enter ', last, ' values:');
      for i:=1 to last do read(value[i]);

      up := value[1] < value[2];
      Ok := value[1] <> value[2];
      i := 2;
      while Ok and (i < last) do begin
          Ok := (up and (value[i] > value[i+1])) or
                (not up and (value[i] < value[i+1]));
          up := not up;
          i := i + 1
      end;

      write('Values ');
      if not Ok then write('do not ');
      writeln('form a saw.')

    end.

value fixed value
i stepper
up stepper
Ok one-way flag


11.

    program smoothedAverage (input, output);

    (* Largest average of three consecutive months *)

    var month: integer;               (* Current month            *)
        current, previous, preceding, (* Data for three months    *)
        average,                      (* Current average          *)
        largest: real;                (* Largest one found so far *)

    begin

        write('Enter 1. value: '); readln(preceding);
        write('Enter 2. value: '); readln(previous);
        write('Enter 3. value: '); readln(current);
        largest := (current + previous + preceding) / 3;

        for month := 4 to 12 do begin
            preceding := previous;
            previous := current;
            write('Enter ', month, '. value: '); readln(current);
            average := (current + previous + preceding) / 3;
            if average > largest then largest := average
        end;

        writeln('Largest three month average was ', largest)

    end.

month stepper
current most-recent holder
previous follower
preceding follower
average temporary
largest most-wanted holder


12.

    program number (input, output);

    (* Read a number with embedded commas, e.g., 123,456 *)

    const
        sep = ',';

    var c:   char;     (* The character read          *)
        val: integer;  (* Accumulated value of number *)

    begin

        val := 0;
        write('Enter number: ');
        read(c);

        while ((c >= '0') and (c <= '9')) or (c = sep) do begin
            if c <> sep
                then val := val * 10 + ord(c) - ord('0');
            read(c)
        end;

        writeln('Value is ', val)

    end.

c most-recent holder
val gatherer


Last updated: February 8, 2006

saja.fi@gmail.com