How can I analyze and understand tricky code in Pascal?

Click For Summary

Discussion Overview

The discussion revolves around analyzing a complex Pascal code snippet provided as a homework problem. Participants explore how to determine the output of the code without executing it, focusing on understanding the flow of variable assignments and function calls. The conversation includes various strategies for code analysis and the challenges posed by the code's structure.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes the original code and expresses confusion about how the output is determined, specifically questioning how a variable can equal 6 given initial values of 1.
  • Another participant suggests a method of numbering lines and creating a table to track variable values as the code executes, emphasizing the tedious nature of this approach.
  • Some participants discuss the idea of rewriting the code with clearer variable names to avoid confusion between function parameters and global variables.
  • One participant proposes inserting multiple writeln statements to trace the values of variables at different points in the code for better understanding.
  • Another participant provides a step-by-step breakdown of the function calls and variable assignments, illustrating how to analyze the code's execution flow.
  • There is a mention of the global scope of the variable 'd' and how it affects the function's behavior, highlighting the complexity of the code.
  • Some participants reflect on the design of the code, suggesting that such convoluted structures are uncommon in professional programming due to best practices in code organization.

Areas of Agreement / Disagreement

Participants express a range of strategies for analyzing the code, but there is no consensus on a single effective method. Some participants agree on the utility of tracking variable values, while others question the effectiveness of rewriting the code with different variable names. The discussion remains unresolved regarding the best approach to understand the output.

Contextual Notes

Participants note the limitations of the original code's design, including potential confusion from variable naming and scope. The complexity of the nested functions and the use of global variables contribute to the difficulty in analyzing the code.

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   Reactions: 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   Reactions: 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   Reactions: 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