Task:
- Problem: Take two non-negative integers and return their product (the result of multiplying the two together).
- The function header should be `int timeser(int x, int y)`
- Restrictions: You are allowed to use anything that `adder` was allowed to use, and also allowed to use `adder` (and of course, `timeser` itself)
#include <iostream>
usingnamespace std;
int adder(int x, int y)//adder
{
if(x==0)
return y;
elseif(y==0)
return x;
elsereturn adder(--x,++y);
}
int timeser(int x,int y)//multiplier
{
if(x==0||y==0)
return 0;
elseif(y==1)
return x;
elseif (x==1)
return y;
elsereturn timeser(--y,adder(x));
}
int main()
{
int x,y;
cout<<"Enter two integers: ";
cin>>x>>y;
cout<<timeser(adder(x,y));
return 0;
}
- Restrictions: You are allowed to use +1 (add 1), -1 (subtract 1), `if`, `else`, `return`, ==, and `adder`.
I just started the recursive section and do not understand much of it.. so obviously practice makes perfect but i need help to learn how to perfect it. Could you help me solve and explain it?
Your base cases look okay, meaning that you shouldn't recurse when one of your values is zero or one. Good. Now you have to figure out the general case. I'll give you a hint... say we want to find the product of 5 and 7:
(5 * 7) == (5 + (5*6))
This is produced when i tried to compile
timeser.cpp: In function `int timeser(int, int)':
timeser.cpp:4: error: too few arguments to function `int adder(int, int)'
timeser.cpp:21: error: at this point in file
timeser.cpp: In function `int main()':
timeser.cpp:29: error: expected `;' before "return"
timeser.cpp:30:2: warning: no newline at end of file
Any ideas why? i really am not use to the recursive stuff yet. I am still learning and trying Thank you for the advice before