• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

Recursion

Alright, so I need some help here, I have to create a recursion method that multiplies 2 numbers a & b, both integers. The method has to take in the parameters (int a, int b). So method names is just public int multrecur(int a, int b), just for reference. But I can't figure out for the life of me how to do it. I can work with recursions though, I made one that outputted a fibonnaci number, but I just can't seem to figure out the algorithm for this one.

Also it has the precondition: 0 <= a <= 10; 0 <= b <= 10

What I have is probably completely off, but here's what I got:
Code:
public int multrecur(int a, int b)
    {
    	if(a<=0 || 10<=a)
    	{
    		return 0;
    	}
    	else
    	{
    		if(b<=0 || 10<=b)
    		{
    			return 0;
    		}
    		else
    		{
    			return multrecur(a+a, b-1);
    		}
    	}
    }

and its being called with:
Code:
System.out.println(fb.multrecur(3,5));
So 15 should be printed, but I always get 0.
 

Moca

New Member
return 0 is the only exit that your function has, so it will never return anything aside from zero at this point. You need to add some logic that returns the value you want.

In addition to that, you are doubling the value of 'a' every function call, so you aren't getting a * b, but a*(2^b).

The way you are currently doing it, one suggestion would be to create a function with three parameters int multiplyRecursive(int a, int b, int product) and add 'a' to 'product' every function call.



Also, your logic doesn't follow up with what you said the restrictions on 'a' and 'b' are:

0 <= a <= 10; 0 <= b <= 10

doesn't match up with
Code:
...
    	if(a<=0 || 10<=a)
    	{
    		return 0;
    	}
    	else
    	{
    		if(b<=0 || 10<=b)
...
 

karnbmx

ceebs. :)
return 0 is the only exit that your function has, so it will never return anything aside from zero at this point. You need to add some logic that returns the value you want.

In addition to that, you are doubling the value of 'a' every function call, so you aren't getting a * b, but a*(2^b).

The way you are currently doing it, one suggestion would be to create a function with three parameters int multiplyRecursive(int a, int b, int product) and add 'a' to 'product' every function call.



Also, your logic doesn't follow up with what you said the restrictions on 'a' and 'b' are:

0 <= a <= 10; 0 <= b <= 10

doesn't match up with
Code:
...
    	if(a<=0 || 10<=a)
    	{
    		return 0;
    	}
    	else
    	{
    		if(b<=0 || 10<=b)
...

He is right. Your logic doesn't add up. Refine your logic and then try again.

EDIT: Try this website. It clears up the Multiplication aspect of recursion for you:

http://www.seas.gwu.edu/~csci133/fall04/133f04recursion.html
 

Roe

Well-Known Member
Code:
int recMult(int a, int b){ if(b > 0) return recMult(a+a, b-1);}
That wouldn't work, you're just going to be adding the result of a+a onto a+a, e.g. 5*5, 5+5 = 10, 10+10 = 20, etc.
 

A_Nub

Developer
Doh, looked at his code xD and wasn't thinking.

Code:
int recMult(int a, int b){return recMultHelper(a,b,a);}
int recMultHelper(int a, int b, int c){if(b > 0) return recMultHelper(a + c, b-1,c);}
 

coyotebean

New Member
Code:
int mul(int a,int b) { if (a==0) return 0 else return mul(a-1,b)+b }
:cool:
 
Top