Introduction to the Pascal Programming Language
© 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/intro_pencil-05.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-05.p
}
{ The purpose of this program is to:
1. Demonstrate the use of real variables and how these
variables can appear when printed using simple format
operations.
2. Demonstrate how a variable can easily change value, if
appropriately directed in the program.
3. Demonstrate the use of e-notation.
}
program Demonstrate_the_use_of_Reals_and_E_Notation;
var A,B,C,D : real;
begin
A := 1.234;
B := A + 5.678;
C := B - A;
D := (9.101) * (A/B);
write(' A = ',A:6); writeln(' A = ',A:6:3);
write(' B = ',B:6); writeln(' B = ',B:6:3);
write(' C = ',C:6); writeln(' C = ',C:6:3);
write(' D = ',D:6); writeln(' D = ',D:6:3); writeln;
D := (4.5) / (B * 6.78);
write(' A = ',A:6); writeln(' A = ',A:6:3);
write(' B = ',B:6); writeln(' B = ',B:6:3);
write(' C = ',C:6); writeln(' C = ',C:6:3);
write(' D = ',D:6); writeln(' D = ',D:6:3); writeln;
D := B - (A/B);
write(' A = ',A:6); writeln(' A = ',A:6:3);
write(' B = ',B:6); writeln(' B = ',B:6:3);
write(' C = ',C:6); writeln(' C = ',C:6:3);
write(' D = ',D:6); writeln(' D = ',D:6:3); writeln;
end. { intro_pencil-05.p }
% pc PASCAL/intro_pencil-05.p
% a.out
A = 1.2e+00 A = 1.234
B = 6.9e+00 B = 6.912
C = 5.7e+00 C = 5.678
D = 1.6e+00 D = 1.625
A = 1.2e+00 A = 1.234
B = 6.9e+00 B = 6.912
C = 5.7e+00 C = 5.678
D = 9.6e-02 D = 0.096
A = 1.2e+00 A = 1.234
B = 6.9e+00 B = 6.912
C = 5.7e+00 C = 5.678
D = 6.7e+00 D = 6.733