How can I analyze and understand tricky code in Pascal?

In summary, the conversation is about a problem in a 10-minute test in school written in Pascal. The task is to analyze and determine the output without writing it down. The code provided is confusing and difficult to understand, but one effective way to solve it is to number the lines and create a table to keep track of the changing values of the variables. This method is time-consuming and highlights the difficulty of being a computer.
  • #1
doktorwho
181
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
  • #2
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
  • #3
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..
 
  • #4
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:
  • #5
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.
 
  • #6
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
  • #7
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 :-)
 
  • #8
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

1. How do I begin analyzing code?

To begin analyzing code, you should first understand the purpose and goal of the code. Then, start by reading through the code line by line to get a general understanding of the structure and logic. It can also be helpful to create comments or notes as you go along.

2. What should I look for when analyzing code?

When analyzing code, you should look for any errors or bugs that may be present. This can include syntax errors, logical errors, or any other type of issue that may cause the code to not function correctly. You should also look for any inefficiencies or areas where the code can be improved for better performance.

3. How can I test the code while analyzing it?

One way to test the code while analyzing it is to use a debugging tool. This will allow you to step through the code and see how it is being executed, as well as track any variables or values. You can also manually test the code by providing different inputs and checking the outputs to ensure the code is functioning correctly.

4. What are some common techniques for analyzing code?

Some common techniques for analyzing code include code reviews, static code analysis, and dynamic code analysis. Code reviews involve having another person or a team review the code for errors or improvements. Static code analysis uses tools to scan the code for potential issues. Dynamic code analysis involves executing the code and monitoring its behavior.

5. How can I improve my code analysis skills?

To improve your code analysis skills, it is important to practice regularly and stay updated on new technologies and techniques. You can also learn from experienced programmers or take courses and workshops focused on code analysis. Additionally, actively seeking feedback and continuously analyzing and improving your own code can also help improve your skills.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
947
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top