Monday, January 13, 2020

33209: School -- Shoe Sizes and Pascal

Chapter 3.2: School -- Shoe Sizes and BASIC

Chapter 3.3: School -- Shoe Sizes and Pascal

Professor Bright said, "We've been able to get a Pascal compiler from Berkeley, and we rewrote this program in Pascal to test it."

"Professor Bright did the Pascal, not me."

Professor Bright tilted his head. "We worked together to get the compiler running and tested, and I was at the keyboard when we wrote the code, but I think we did the Pascal rewrite together."

Professor Crane sighed. "Maybe. Whatever. Anyway, we wanted to give y'all a little advance notice of of the Pascal class we plan to offer next fall, and invite you all to enroll. We'll be teaching it together the first time through, so we don't miss anything." 

"We'll tell you more about Pascal and the course later," Professor Bright added.

Professor Bright told Pat how to load and list the Pascal program on the Univac operating system. Here's what the program looked like:

program shoesz;

{ Print shoe size table by Thomas Bright }

var conv: Char;
  st_sz, end_sz, sz: Real;
  dbl_sz: Integer;

const cm_in = 2.54;

begin
  conv := ' ';
  while (conv<>'E') AND (conv<>'M') do
  begin
    writeln ('Shoe Sizes');
    writeln ('Type E for English to Metric.');
    writeln ('Type M for Metric to English: ');
    readln (conv)
  end;

  writeln ('Start: ');
  readln (st_sz);
  writeln ('End: ');
  readln (end_sz);

  if conv = 'E' then
    writeln ('English => Metric')
  else
    writeln ('Metric => English');

  { Step by halves. }
  for dbl_sz := round(st_sz)*2 to round(end_sz)*2 do 
  begin
    sz := dbl_sz/2.0;
    write (sz, chr(9), '=>');
    if conv = 'E' then
      writeln (sz*cm_in)
    else
      writeln (sz/cm_in)
  end
end.

(I used Free Pascal on a Linux OS to check this code, and it compiles and runs. Most Pascal compilers should compile a runnable object from it okay.)

 "So, what do you think?" Professor Crane asked. "Isn't this easier to read?"

"I'm still not sure I know what it means," Andrea complained.

"There's no line numbers. How does the computer know what to do next?" Theo mused.

"I think the computer follows it in the sequence it's written in," Daren posited.

"Does the indenting mean anything?" I might have been the one that asked that.

"No, the indenting is just to make it easier to read," answered Professor Bright. "The begin and end keywords show where the blocks begin and end. Otherwise, the control keywords and the punctuation mark the flow."

"I'll show you on the whiteboard, later," Professor Crane added.

"Do you have to say what the variables are up there at the top?" Mike asked.

"Good question, and yes," Professor Bright answered. "Variables and constants are declared before the procedure or function block begins."

Pat was silent, examining the code on the screen in front of her. Then she looked up and asked, "Can we run this? Do we have to compile it first?"

The professors looked at each other. 

Professor Bright tilted his head and raised his eyebrows. "Let's have Pat walk through the compile step, so everyone can watch. Then she can run it."

Professor Crane nodded in agreement, and Professor Bright told Pat what to type while we watched. It compiled quickly, and she linked the compiled object code, loaded it, and ran it:

Shoe Sizes
Type E for English to Metric.
Type M for Metric to English: 
M
Start: 
13
End: 
33
Metric => English
 1.3000000000000000E+001	=> 5.11811023622047244104E+0000
 1.3500000000000000E+001	=> 5.31496062992125984249E+0000
 1.4000000000000000E+001	=> 5.51181102362204724436E+0000
 1.4500000000000000E+001	=> 5.70866141732283464581E+0000
 1.5000000000000000E+001	=> 5.90551181102362204725E+0000
 1.5500000000000000E+001	=> 6.10236220472440944870E+0000
 1.6000000000000000E+001	=> 6.29921259842519685058E+0000
 1.6500000000000000E+001	=> 6.49606299212598425202E+0000
 1.7000000000000000E+001	=> 6.69291338582677165347E+0000
 1.7500000000000000E+001	=> 6.88976377952755905535E+0000
 1.8000000000000000E+001	=> 7.08661417322834645679E+0000
 1.8500000000000000E+001	=> 7.28346456692913385824E+0000
 1.9000000000000000E+001	=> 7.48031496062992126012E+0000
 1.9500000000000000E+001	=> 7.67716535433070866156E+0000
 2.0000000000000000E+001	=> 7.87401574803149606301E+0000
 2.0500000000000000E+001	=> 8.07086614173228346445E+0000
 2.1000000000000000E+001	=> 8.26771653543307086590E+0000
 2.1500000000000000E+001	=> 8.46456692913385826821E+0000
 2.2000000000000000E+001	=> 8.66141732283464566965E+0000
 2.2500000000000000E+001	=> 8.85826771653543307110E+0000
 2.3000000000000000E+001	=> 9.05511811023622047254E+0000
 2.3500000000000000E+001	=> 9.25196850393700787399E+0000
 2.4000000000000000E+001	=> 9.44881889763779527543E+0000
 2.4500000000000000E+001	=> 9.64566929133858267688E+0000
 2.5000000000000000E+001	=> 9.84251968503937007919E+0000
 2.5500000000000000E+001	=> 1.00393700787401574806E+0001
 2.6000000000000000E+001	=> 1.02362204724409448821E+0001
 2.6500000000000000E+001	=> 1.04330708661417322835E+0001
 2.7000000000000000E+001	=> 1.06299212598425196850E+0001
 2.7500000000000000E+001	=> 1.08267716535433070864E+0001
 2.8000000000000000E+001	=> 1.10236220472440944887E+0001
 2.8500000000000000E+001	=> 1.12204724409448818902E+0001
 2.9000000000000000E+001	=> 1.14173228346456692916E+0001
 2.9500000000000000E+001	=> 1.16141732283464566931E+0001
 3.0000000000000000E+001	=> 1.18110236220472440945E+0001
 3.0500000000000000E+001	=> 1.20078740157480314960E+0001
 3.1000000000000000E+001	=> 1.22047244094488188974E+0001
 3.1500000000000000E+001	=> 1.24015748031496062997E+0001
 3.2000000000000000E+001	=> 1.25984251968503937012E+0001
 3.2500000000000000E+001	=> 1.27952755905511811026E+0001
 3.3000000000000000E+001	=> 1.29921259842519685040E+0001

"Huh? One point three what?" Theo complained.

"Scientific notation," I ventured.

"Sales isn't going to like that," George frowned and shook his head.

"Maybe use a subroutine like the second BASIC program," Mike suggested.

"And I happen to have just that," replied Professor Bright. He had Pat load and list another Pascal program.

program shoesz;

{ Print shoe size table with halves, by Thomas Bright and Rusty Crane }

procedure wrhalf( size: Real );
var
  ipart, numer: Integer;
begin
  ipart := trunc(size);
  numer := trunc((size-ipart+0.25)*2);
  if numer > 1 then
    begin
      numer := 0;
      ipart := ipart+1;
    end;
  write (ipart);
  if numer > 0 then 
    write (' 1/2')
end;

var conv: Char;
  st_sz, end_sz, sz, conv_sz: Real;
  dbl_sz: Integer;

const cm_in = 2.54;

begin
  conv := ' ';
  while (conv<>'E') AND (conv<>'M') do
  begin
    writeln ('Shoe Sizes');
    writeln ('Type E for English to Metric.');
    writeln ('Type M for Metric to English: ');
    readln (conv)
  end;

  writeln ('Start: ');
  readln (st_sz);
  writeln ('End: ');
  readln (end_sz);

  if conv = 'E' then
    writeln ('English => Metric')
  else
    writeln ('Metric => English');

  { Step by halves. }
  for dbl_sz := round(st_sz)*2 to round(end_sz)*2 do 
  begin
    sz := dbl_sz/2.0;
    if conv = 'E' then
      conv_sz := sz*cm_in
    else
      conv_sz := sz/cm_in;
    wrhalf(sz);
    write (chr(9), '=> ');
    wrhalf(conv_sz);
    writeln (chr(9), '(', sz , '=> ', conv_sz, ')')
  end
end.

"Procedure means subroutine?" Pat asked.

"One kind," replied Professor Bright. "There are functions, as well, which return a value. Procedures do not return values in Pascal."

Mike said, "That procedure is a lot easier to see where it starts and ends than the BASIC subroutine. "

"Precisely the point," Professor Bright responded.

Without being instructed, Pat proceeded to compile, link, load, and run the program:

Shoe Sizes
Type E for English to Metric.
Type M for Metric to English: 
M
Start: 
13
End: 
33
Metric => English
13	=> 5	( 1.3000000000000000E+001=>  5.1181102362204722E+000)
13 1/2	=> 5 1/2	( 1.3500000000000000E+001=>  5.3149606299212602E+000)
14	=> 5 1/2	( 1.4000000000000000E+001=>  5.5118110236220472E+000)
14 1/2	=> 5 1/2	( 1.4500000000000000E+001=>  5.7086614173228343E+000)
15	=> 6	( 1.5000000000000000E+001=>  5.9055118110236222E+000)
15 1/2	=> 6	( 1.5500000000000000E+001=>  6.1023622047244093E+000)
16	=> 6 1/2	( 1.6000000000000000E+001=>  6.2992125984251972E+000)
16 1/2	=> 6 1/2	( 1.6500000000000000E+001=>  6.4960629921259843E+000)
17	=> 6 1/2	( 1.7000000000000000E+001=>  6.6929133858267713E+000)
17 1/2	=> 7	( 1.7500000000000000E+001=>  6.8897637795275593E+000)
18	=> 7	( 1.8000000000000000E+001=>  7.0866141732283463E+000)
18 1/2	=> 7 1/2	( 1.8500000000000000E+001=>  7.2834645669291342E+000)
19	=> 7 1/2	( 1.9000000000000000E+001=>  7.4803149606299213E+000)
19 1/2	=> 7 1/2	( 1.9500000000000000E+001=>  7.6771653543307083E+000)
20	=> 8	( 2.0000000000000000E+001=>  7.8740157480314963E+000)
20 1/2	=> 8	( 2.0500000000000000E+001=>  8.0708661417322833E+000)
21	=> 8 1/2	( 2.1000000000000000E+001=>  8.2677165354330704E+000)
21 1/2	=> 8 1/2	( 2.1500000000000000E+001=>  8.4645669291338574E+000)
22	=> 8 1/2	( 2.2000000000000000E+001=>  8.6614173228346463E+000)
22 1/2	=> 9	( 2.2500000000000000E+001=>  8.8582677165354333E+000)
23	=> 9	( 2.3000000000000000E+001=>  9.0551181102362204E+000)
23 1/2	=> 9 1/2	( 2.3500000000000000E+001=>  9.2519685039370074E+000)
24	=> 9 1/2	( 2.4000000000000000E+001=>  9.4488188976377945E+000)
24 1/2	=> 9 1/2	( 2.4500000000000000E+001=>  9.6456692913385833E+000)
25	=> 10	( 2.5000000000000000E+001=>  9.8425196850393704E+000)
25 1/2	=> 10	( 2.5500000000000000E+001=>  1.0039370078740157E+001)
26	=> 10	( 2.6000000000000000E+001=>  1.0236220472440944E+001)
26 1/2	=> 10 1/2	( 2.6500000000000000E+001=>  1.0433070866141732E+001)
27	=> 10 1/2	( 2.7000000000000000E+001=>  1.0629921259842520E+001)
27 1/2	=> 11	( 2.7500000000000000E+001=>  1.0826771653543307E+001)
28	=> 11	( 2.8000000000000000E+001=>  1.1023622047244094E+001)
28 1/2	=> 11	( 2.8500000000000000E+001=>  1.1220472440944881E+001)
29	=> 11 1/2	( 2.9000000000000000E+001=>  1.1417322834645669E+001)
29 1/2	=> 11 1/2	( 2.9500000000000000E+001=>  1.1614173228346457E+001)
30	=> 12	( 3.0000000000000000E+001=>  1.1811023622047244E+001)
30 1/2	=> 12	( 3.0500000000000000E+001=>  1.2007874015748031E+001)
31	=> 12	( 3.1000000000000000E+001=>  1.2204724409448819E+001)
31 1/2	=> 12 1/2	( 3.1500000000000000E+001=>  1.2401574803149606E+001)
32	=> 12 1/2	( 3.2000000000000000E+001=>  1.2598425196850394E+001)
32 1/2	=> 13	( 3.2500000000000000E+001=>  1.2795275590551181E+001)
33	=> 13	( 3.3000000000000000E+001=>  1.2992125984251969E+001)

George nodded approvingly. "I think the sales department will find that easier to read."

"Especially if we remove the scientific notation stuff on the end of each line," added Alex. 

Mike commented, "We could put that last write-line in a comment and put an empty write-line in its place, and the scientific notation output would be gone, but easy to bring back."

"Having the output in its own procedure makes it easier to modify, doesn't it?" Professor Bright pointed out. "Much easier to see what to remove and what to change."

Professor Crane said, "Okay, now we've had an advance look at how to use the terminals, and at Pascal. Let's go back to the classroom and use the whiteboard and the overhead projector to get a better look at this."

Again, there were complaints and jests from the students as we filed out and returned to the classroom. 

TOC

[Current backup at https://joel-rees-economics.blogspot.com/2020/01/bk-33209-school-shoe-sizes-and-pascal.html.
Developed January through March 2021, notes at https://joel-rees-economics.blogspot.com/2020/01/notes-33209-school-basic-vs-pascal-vs.html.
Extracted and expanded from
https://joel-rees-economics.blogspot.com/2020/01/bk01-33209-school.html.
Earlier backup at https://joel-rees-economics.blogspot.com/2020/01/bk-33209-school.html.]

No comments:

Post a Comment

33209: Discovering the 6800 -- Parents and Polygamy

A Look at the 8080/TOC "Whoa, Merry, look who's here!" Jim said, sotto voce. He, Roderick, and I were at our lab table ...