### Could I get some help?

- 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)
 ``12345678910111213141516171819202122232425262728293031`` ``````#include using namespace std; int adder(int x, int y)//adder { if(x==0) return y; else if(y==0) return x; else return adder(--x,++y); } int timeser(int x,int y)//multiplier { if(x==0||y==0) return 0; else if(y==1) return x; else if (x==1) return y; else return timeser(--y,adder(x)); } int main() { int x,y; cout<<"Enter two integers: "; cin>>x>>y; cout<

- 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?
Last edited on
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))
Last edited on
i forgot to add: - Restrictions: You are allowed to use +1 (add 1), -1 (subtract 1), `if`, `else`, `return`, ==, and `adder`.
Right. So translate that hint I gave. Expressed even more explicitly:
5 * 7 equals...
5 + (5 * 6)...
5 + (5 * (7-1))...
adder(5, (5 * (7 - 1))...
Last edited on
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
Thank you for the help i fixed the problem!
Topic archived. No new replies allowed.