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-20.p

{ Advanced Pascal Programming Language Exercises
  Dr. Thomas W. MacFarland
  advanced_pencil-20.p
}

{ The purpose of this program is to:

  1.  Demonstrate the use of sets in Standard Pascal.

  2.  Demonstrate math operations with sets.
}

program Demonstrate_Sets;   

type Product = (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O);

     Item = set of Product;

var  Z_1   : Item;
     Z_2   : Item;
     Z_3   : Item;
     Z_4   : Item;
     Z_5   : Item;
     Z_6   : Item;

begin {Main Program}
   Z_1 := [A, B];
   Z_2 := [A, B, D, E,];
   Z_3 := [A..D];
   Z_4 := Z_2 + [F];
   Z_5 := [A, D, F, G];

   Z_6 := Z_1 + Z_2 + Z_3 + Z_4 + Z_5;

   writeln;
   writeln('Z_6 := Z_1 + Z_2 + Z_3 + Z_4 + Z_5'); writeln;

   if A in Z_6 then write('A');
   if B in Z_6 then write('B');
   if C in Z_6 then write('C');
   if D in Z_6 then write('D');
   if E in Z_6 then write('E');
   if F in Z_6 then write('F');
   if G in Z_6 then write('G');
   if H in Z_6 then write('H');
   if I in Z_6 then write('I');
   if J in Z_6 then write('J');
   if K in Z_6 then write('K');
   if L in Z_6 then write('L');
   if M in Z_6 then write('M');
   if N in Z_6 then write('N');
   if O in Z_6 then write('O'); writeln;

   Z_6 := [A..O] - Z_6; 

   writeln;
   writeln ('Z_6 := [A..O] - Z_6'); writeln;

   if A in Z_6 then write('A');
   if B in Z_6 then write('B');
   if C in Z_6 then write('C');
   if D in Z_6 then write('D');
   if E in Z_6 then write('E');
   if F in Z_6 then write('F');
   if G in Z_6 then write('G');
   if H in Z_6 then write('H');
   if I in Z_6 then write('I');
   if J in Z_6 then write('J');
   if K in Z_6 then write('K');
   if L in Z_6 then write('L');
   if M in Z_6 then write('M');
   if N in Z_6 then write('N');
   if O in Z_6 then write('O'); writeln;

   Z_6 := [A..O] + Z_6; 

   writeln;
   writeln ('Z_6 := [A..O] + Z_6'); writeln;

   if A in Z_6 then write('A');
   if B in Z_6 then write('B');
   if C in Z_6 then write('C');
   if D in Z_6 then write('D');
   if E in Z_6 then write('E');
   if F in Z_6 then write('F');
   if G in Z_6 then write('G');
   if H in Z_6 then write('H');
   if I in Z_6 then write('I');
   if J in Z_6 then write('J');
   if K in Z_6 then write('K');
   if L in Z_6 then write('L');
   if M in Z_6 then write('M');
   if N in Z_6 then write('N');
   if O in Z_6 then write('O'); writeln;

end.  {advanced_pencil-20.p}
%  pc PASCAL/advanced_pencil-20.p
%  a.out

Z_6 := Z_1 + Z_2 + Z_3 + Z_4 + Z_5

ABCDEFG

Z_6 := [A..O] - Z_6

HIJKLMNO

Z_6 := [A..O] + Z_6

ABCDEFGHIJKLMNO

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.