2

This was a homework question I had.

Consider this java pseudocode

string rep (int n){ ---> n is a natural number in this case.

if(n==0)
return "0";
if(n==1)
return "1";
string w = rep(n/2);
if(n%2==0)
  return append (w,'0');
 else
 return append (w,'1');
}

If I call this method on argument (decimal) 637, what is the first two(decimal)-digit argument of a recursive call?

I am confused on what an argument means in this instance. It seems that I can just divide 637 by 8 and get 79. Would this be correct?

5
  • What is append ? And what should the pseudocode do ? Commented Oct 29, 2015 at 17:31
  • 1
    The value you pass as n is an argument. This program converts a number to binary. Commented Oct 29, 2015 at 17:31
  • @Cyrbil I assume its "short hand" for w + c Commented Oct 29, 2015 at 17:32
  • Yes. Appends means "+" Commented Oct 29, 2015 at 17:33
  • Yes, your answer would be correct. It's actually very easy to verify: transform this code to real Java code, add a System.out.println(n); instruction at the beginning of the method, and see what the first 2-digit printed number is. Commented Oct 29, 2015 at 17:40

3 Answers 3

2

In your case, the argument is the n.

In general, the arguments (or argument list for that matter) for a method call is the things which you put between the brackets (here):

myMethod(a, b, c);

In this example, a, b, and c are arguments. Often people also refer to them as parameters. That's the same in this context.

Now your question goes on to what the first two arguments of the recursive calls are. For this, not that in line string w = rep(n/2); is the recursive call. Thus, the first recursive call will have argument n/2, which is the rounded-down part of 637/2 = 318. In turn, the next recursive call will be 318/2 = 159.

Sign up to request clarification or add additional context in comments.

3 Comments

n is the parameter, the value you give it is the argument.
@PeterLawrey You're right. I just think it will take years before this matters to a beginner.
True, I would say "the argument is in the n" ;)
1

An argument is a parameter passed to a function.
In your case the function is rep and the argument is n

Comments

0

First argument is 637. n=637. It isn't equal to 0. It isn't equal to 1. w=rep(n/2) => w=rep(637/2) => w=rep(318.5). That would be the first recursive call, and its argument would be 318.5, so the answer would be 31

1 Comment

integer division -> 318 and not 318.5. And 318 is not a 2-digit number. It has 3 digits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.