Advanced Pascal Programming Language Problems

© 1998 by Dr. Thomas W. MacFarland -- All Rights Reserved


     Instructions:   Review the following program and see if you can 
                     predict the outcome by completing a "pencil 
                     trace" of the code.  

                     Program output is provided at the end of this 
                     page.

     Compiler:       The program was prepared using Standard Pascal 
                     on a UNIX-based host computer.


% cat PASCAL/advanced_pencil-17.p
{ Advanced Pascal Programming Language Exercises
  Dr. Thomas W. MacFarland
  advanced_pencil-17.p
}

{ The purpose of this program is to:

  1.  Demonstrate the use procedures.
}

program Demonstrate_the_Use_of_Procedures_to_Work_with_Values;

const  Maximum_Enter = '90';
       Maximum_Length = 5;

type   string = packed array [1..12] of char;

var    Int_S : string;
       Int_I : integer;
       OK    : boolean;

procedure Get_String (var list : string);
var i : integer;
    c : char;
begin
  for i := 1 to 10 do
    list[i] := ' ';
  i := 1;
  while not eoln do
    begin
        read(c);
        list[i] := c;
        i := i + 1;
    end;
  readln;
end; { Get_String }


function Length (line : string) : integer;
var i : integer;
begin
  i := 10;
  while line[i] = ' ' do
    i := i - 1;
  Length := i;
end;  (* Length *)


procedure Remove (var line : string; start, size : integer);
var   i, j    : integer;
begin
  for i := start + size to 10 do
    begin
      line [start] := line [i];
      start := start + 1;
    end;
  for j := start to 10 do
    line [j] := ' ';
end;


procedure  Value (Data      : string;
               var Number : integer;
               var Ok     : boolean);
const  Plus  = 1;
       Minus = -1;
var    Sign, Len, Tens, i : integer;
begin                                     (* Initialize *)
  Ok     := true;
  Tens   := 1;
  Number := 0;
  Sign   := 0;
  Len    := Length (Data);
  if Len = 0 then 
    Ok := false                           (* Check for null string *)
  else
    if Data [1] = '+' then                (* Check for + or - sign *)
      Sign := Plus
    else 
      if Data [1] = '-' then
        Sign := Minus;
  if abs(Sign) = 1 then             (* If + or - sign is present,  *)
    if Len = 1 then                 (* check if Length is greater  *)
      Ok := false                   (* than 1.  If so, then Remove *)
    else                            (* the sign from Data.         *)
      begin
        Remove (Data, 1, 1);
        Len := Length (Data);
      end;
  if (Length (Data) >= Maximum_Length) and ( Data > Maximum_Enter) then
    Ok := false;                    (* Number is out of range      *)
  if Ok then                        (* Begin conversion to INTEGER *)
    for i := Len downto 1 do
      if (Data[i] < '0') or (Data[i] > '9') then
        Ok := false
      else                          (* Character is valid number   *)
        begin
          Number := Number + (ord(Data[i]) - ord('0')) * Tens;
          Tens := Tens * 10;        (* Increment decimal place     *)
        end;
  if Sign <> 0 then
    Number := Number * Sign;        (* Adjust sign if negative     *)
end;  (*  Value  *)


begin  (* Main Program *)

  repeat                           (* Loop to accept a valid integer *)
    write('Enter an integer:      ');
    Get_String(Int_S);
    Value (Int_S, Int_I, OK);
  until OK;

  writeln ('Valid integer: ', Int_I:9);
end.  {advanced_pencil-17.p}
%  pc PASCAL/advanced_pencil-17.p
%  a.out
Enter an integer:      9
Valid integer:         9

Please send comments or suggestions to Dr. Thomas W. MacFarland

There have been [an error occurred while processing this directive] visitors to this page since February 1, 1999.