[comp.lang.pascal] Another TPascal puzzle

prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) (06/26/91)

Ok, I admit I didn't clearly point out the last puzzle was not a problem
I had. To all the people who kindly explained my "error" to me, many thanks.

But what I actually wanted to start was sort of a "Mind-Buster" topic in
this group to make other people have some fun puzzling out things one
encountered in one's long and hard way of pascal programming (you know,
even compilers that do serious checking sometimes don't know what you
want them to do :-)

So I would prefer not to read all the solution in public in this group
but either have some email (always appreciated) or better yet have
some follow-up puzzle posted to this group. There must be virtually
zillions of people having gotten a _headache_ to try to figure out
what exactly the compiler misunderstands on that obvious part of code...

Enough talk, here's another one (should work on any ansi-pascal compiler,
but you never know these days...)

program why_ten?
(* this is an example why even clearly defined scopes can't help
   sometimes; better: don't use duplicate variable name as long
   as possible *)

var
  x,y : byte;

procedure Outer;
  var
    y : byte;
  
  procedure Inner;  (* show a multiplication table *)
    begin
      for x:=1 to 10 do begin
        for y:=1 to y do
          write(x*y:5);
        writeln;
      end;
    end;

  begin (* of Outer *)
    for y:=1 to 10 do (* 10 linefeeds *)
      writeln;
    Inner;
  end;

begin (* of why_ten? *)
  Outer;
end.

What you would expect to see on your screen on running this program
would perhaps be ten empty lines and then a multiplication tables
looking somehow like this:
    1    2    3    4    5    6    7    8    9   10
    2    4    6 ...
    3    6  ..
    :
   10   20   30 ..

Actually only the last line will be output, but ten times in a row!
But then, if only the tenth run of the x-loop is executed, why does
it occur exactly ten times?

Hope you enjoy this one (actually it _is_ bad programming, send your
flames to alt.flame), hope also to see many more puzzles, me and my
compiler will watch the goings with great interest, Peter

---
-------------------------------------------------------------------------
sig? You must be kidding with all those SIGKILLs around on this machine!
Peter Knoerrich,        email: prknoerr@faui43.informatik.uni-erlangen.de

prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) (06/27/91)

prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) writes:

Sorry there actually _IS_ a heavy typing error in the program code,
I intended to run the Inner y-loop from 1 to 10! Actually, without
trying I would not be able to predict what happened to a loop:
"for y:=1 to y do ...". So here is the correct version:

>program why_ten?
>(* this is an example why even clearly defined scopes can't help
>   sometimes; better: don't use duplicate variable name as long
>   as possible *)

>var
>  x,y : byte;

>procedure Outer;
>  var
>    y : byte;
>  
>  procedure Inner;  (* show a multiplication table *)
>    begin
>      for x:=1 to 10 do begin
>        for y:=1 to 10 do
> (*                 ^^  this must be a ten! *)
>          write(x*y:5);
>        writeln;
>      end;
>    end;

>  begin (* of Outer *)
>    for y:=1 to 10 do (* 10 linefeeds *)
>      writeln;
>    Inner;
>  end;

>begin (* of why_ten? *)
>  Outer;
>end.

---
sig? You must be kidding with all those SIGKILLs around on this machine!
Peter Knoerrich,        email: prknoerr@faui43.informatik.uni-erlangen.de