Could I get some help?

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
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<<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?
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))...
adder(5, (5 * (--7))...
adder(5, timeser(5, (--7)))
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.