1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to analyze this code?

  1. Jan 3, 2017 #1
    1. The problem statement, all variables and given/known data
    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.
    2. Relevant equations
    3. The attempt at a solution

    I wrote it in the editor and it looks like this:
    Code (Pascal):

    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 (Pascal):

    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 (Pascal):

    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?
     
  2. jcsd
  3. Jan 3, 2017 #2

    jedishrfu

    Staff: Mentor

    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 (Text):

    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. :-)
     
  4. Jan 3, 2017 #3
    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 dont get how do i get from the 3rd line of the function that B ( or C in original) is 6..
     
  5. Jan 3, 2017 #4

    QuantumQuest

    User Avatar
    Gold Member

    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 (Text):
    writeln(...)
    statements as you need intermittently in order to see how the answer is got.

    EDIT: Also, you can put every kind of
    Code (Text):
    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 (Text):
    writeln(...)
    statement(s) or "Result of function x is..." - you got the notion.
     
    Last edited: Jan 3, 2017
  6. Jan 3, 2017 #5

    NascentOxygen

    User Avatar

    Staff: Mentor

    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.
     
  7. Jan 3, 2017 #6

    jedishrfu

    Staff: Mentor

    Here's another way to do it:
    Code (Text):

    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=???
     
     
  8. Jan 3, 2017 #7
    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 :-)
     
  9. Jan 3, 2017 #8

    jedishrfu

    Staff: Mentor

    Also notice the d variable isnt passed in to 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: Jan 3, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted