How can I analyze and understand tricky code in Pascal?

Click For Summary
The discussion focuses on analyzing a complex Pascal program to determine its output, specifically how the value 6 is derived. Participants suggest using a line-numbering method and creating a table to track variable values throughout the code execution. They emphasize the importance of inserting writeln statements to debug and understand the flow of the program. Additionally, renaming variables for clarity and recognizing the scope of global variables are recommended strategies. Overall, the conversation highlights the challenges of deciphering convoluted code and the need for organized coding practices.
doktorwho
Messages
181
Reaction score
6

Homework Statement


This is a problem of the 10-minute test we had today in school.
Capture.JPG

Its written in Pascal and it is asked of us to analyze it and figure out what is the output when it runs without writing it down.

Homework Equations


3. The Attempt at a Solution [/B]
I wrote it in the editor and it looks like this:
Code:
program jan2015(output);
var
  a,b,c,d:integer;

function f1(var a:integer; b:integer; var c:integer):integer;
         function f2(var a,b:integer):integer;
         begin
           c:=a+d;
           b:=a+1;
           d:=d+1;
           f2:=b
         end;
begin
  c:=a+b;
  d:=c+a;
  f1:=f2(d, b)
end;
begin
  a:=1; b:=1; c:=1; d:=1;
  writeln(output, f1(c, a, b), a, b, c, d);
  readln();
end.

The purpose of readln() is to maintain the window open rather that it closing. The result is given as the answer (C) but i do not understand how. I tried putting a writeln function it between to see how it evolves to that answer but am still confused.

Code:
program jan2015(output);
var
  a,b,c,d:integer;

function f1(var a:integer; b:integer; var c:integer):integer;
         function f2(var a,b:integer):integer;
         begin
           c:=a+d;
           writeln('This is C ',c); {it is 6! when i run it, how is it 6? when the starting values are 1-s?}
           readln();
           b:=a+1;
           d:=d+1;
           f2:=b
         end;
begin
  c:=a+b;
  d:=c+a;
  f1:=f2(d, b)
end;
begin
  a:=1; b:=1; c:=1; d:=1;
  writeln(output, f1(c, a, b), a, b, c, d);
  readln();
end.

Since it is on purpose written to confuse us i rewrote it so that the functions contain real given values:

Code:
program jan2015(output);
var
  a,b,c,d:integer;

function f1(var c:integer; a:integer; var b:integer):integer;
         function f2(var c,a:integer):integer;
         begin
           b:=c+d;
           writeln('This is B ',b); {still 6}
           readln();
           a:=c+1;
           d:=d+1;
           f2:=a
         end;
begin
  b:=c+a;
  d:=b+c;
  f1:=f2(d, a)
end;
begin
  a:=1; b:=1; c:=1; d:=1;
  writeln(output, f1(c, a, b), a, b, c, d);
  readln();
end.
[/code

How does one analyze these codes given like this and more important, how is it 6?
 
Physics news on Phys.org
One effective though tedious way is to number the lines of your program and to then make a table where the columns represent the line # you're on and the value of each variable:

Code:
line         a       b       c      d
-------  ------- ------- ------ ------
1            0       0       0       0
...

as you read the code and compute what the variable's new value is given the prior values you write down that line. Eventually you get your result and realize how difficult it is to be a computer doing such mundane and convoluted code. :-)
 
  • Like
Likes QuantumQuest
jedishrfu said:
One effective though tedious way is to number the lines of your program and to then make a table where the columns represent the line # you're on and the value of each variable:

Code:
line         a       b       c      d
-------  ------- ------- ------ ------
1            0       0       0       0
...

as you read the code and compute what the variable's new value is given the prior values you write down that line. Eventually you get your result and realize how difficult it is to be a computer doing such mundane and convoluted code. :-)
Is it a good idea to rewrite the line of the code using the corrected given values rather than keeeping in mind that c is a, b is d and what not?
Also i tried this and still don't get how do i get from the 3rd line of the function that B ( or C in original) is 6..
 
jedishrfu said:
One effective though tedious way is to number the lines of your program and to then make a table where the columns represent the line # you're on and the value of each variable:

as you read the code and compute what the variable's new value is given the prior values you write down that line. Eventually you get your result and realize how difficult it is to be a computer doing such mundane and convoluted code. :-)

That reminds me of my school years - we did just this in Pascal as here. It's definitely a good way and in fact it helps for doing better code evaluation mentally.

For the verification of the answer, I suggest putting as many
Code:
writeln(...)
statements as you need intermittently in order to see how the answer is got.

EDIT: Also, you can put every kind of
Code:
writeln(...)
statement that can help you, like in the first line inside a function: "The function is called with a = ..., b = ..." with the appropriate
Code:
writeln(...)
statement(s) or "Result of function x is..." - you got the notion.
 
Last edited:
doktorwho said:
How does one analyze these codes given like this and more importan
I don't like your idea of the "rewrite", I can't see the point.

Maybe if you renamed the variables in your main program to
var pa,pb,pc,pd : integer;

so they are clearly distinguishable from the parameters & variables in the functions?

Or red, blue, white, orange so they are named totally differently.
 
Here's another way to do it:
Code:
main :    a=1; b=1; c=1; d=1;
     call f1(1,1,1);

f1:   c=1; a=1; b=1;
  b:=c+a;     ----> b=2;
  d:=b+c;     ----> d=3;
  f1:=f2(3, 1)   

f2:     c=3; a=1;
  b=c+d;      ----> b=?
 
  • Like
Likes doktorwho
jedishrfu said:
Here's another way to do it:
Code:
main :    a=1; b=1; c=1; d=1;
     call f1(1,1,1);

f1:   c=1; a=1; b=1;
  b:=c+a;     ----> b=2;
  d:=b+c;     ----> d=3;
  f1:=f2(3, 1)  

f2:     c=3; a=1;
  b=c+d;      ----> b=?
Oh great, i understand now, thanks, you evaluated the f1 first and then did the f2, when i analyzed i went straight to f2 couse it was the first line after the f2 but forgot that you have to call it first. Thanks again :-)
 
Also notice the d variable isn't passed into the as it has a global scope.

This is tricksy code designed to stump the chump I mean student. You seldom this kind of code in the wild as most programmer realize you reap what you sow so you better organize your code by common team coding conventions,some effective comments and even some intermediate results logging features.
 
Last edited:
  • Like
Likes doktorwho

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
Replies
3
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
1K