0

This is the problem

Can someone please help me, I really do not understand how on earth did they get x*y in this procedure ... Question asks what does procedure do ?

    PROGRAM iz52(output);
    VAR pom: integer;
    FUNCTION calc(x,y: integer): integer;
    BEGIN
    IF (y=1) THEN
    calc:= x
    ELSE
    calc:= calc(x, y-1) + x
    END;
    A) x+y 
    B) x*y
    C) x^y
  • Please.... do not link an image of code (especially when their hard for folks like me with bad prescription glasses). Put the code directly in your question. To your question, it's determining if you understand recursive calls. And, yes, the correct answer is `x * y`. Walk through it step by step. Take an example, like `x=2` and `y=3` and pretend you're the computer and get a pencil and paper and work through it. – lurker Jan 10 '18 at 20:42
  • Sorry for putting image, I did not know... Can u please tell me what those calc:=calc(x,y-1) + x; do .... i dont understand calc(x,y-1) part, only that i do not understand ? – Zoran Mladenovski Jan 10 '18 at 20:44
  • Look up `recursion` – tonypdmtr Jan 10 '18 at 20:46
  • Even if you read it, you can tell: `calc(x, y)` is computed by first computing `calc(x, y-1)` (if `y` > 1) then adding `x`. If `y` has the value of 1, then you just get `x` (makes sense: `x` is `x * 1` right?). The `calc(x, y-1)` is calculating the result of `calc` where first argument is `x` and second argument is `y-1`. It's a "recursive" call. – lurker Jan 10 '18 at 20:47
  • `calc(x,y-1)` is a function call. `+ x` is well, addition. `calc :=` assigns to the function return value. Did you go to class at all? – David Heffernan Jan 10 '18 at 20:51
  • Rather than have us tell you the answer (they are looking for B) why not try to work it out for yourself? – David Heffernan Jan 10 '18 at 21:00
  • Yes I did try to work it for myself couple of times, but did not understand the recursion procedure... now I finally think i understand it. For example: y=3 ..... calc(x,3)+x= calc(x,2)+x+x= calc(x,1)+x+x+x = 3x = x*y did I got this correct ? – Zoran Mladenovski Jan 10 '18 at 21:10
  • 2
    Well done. Don't give up too soon. You can do this, and it's better to do it yourself, you will learn more. – David Heffernan Jan 10 '18 at 22:07
  • Plug in values of x and y and look at the results. It might make it more obvious of you created a table with three columns, 'x', 'y', and 'calc', and you pick values of y greater than zero, it should be clear that calc does x*y for values of y greater than zero. – Stuart Jan 13 '18 at 09:44

0 Answers0