I'm stuck on a homework problem. Just not sure where I'm going wrong, I think my code should work and it doesn't, and I'm not sure why.

The assigned problem is:
Recursive Multiplication: Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition (7 * 4 = 7 + 7 + 7 + 7).

And my code:

#include <iostream>
using namespace std;

int myMath(int, int);

int main()
    int num1, num2;
	cout << "Enter an integer.\n";
	cin >> num1;
	cout << "Enter a second integer and I will multiply them.\n";
	cin >> num2;
	cout << "The total is: " << myMath(num1,num2) << ".\n";
	return 0;

int myMath(int x, int y) {
    int total;
    if ( y > 0) {
         total = total + x;
    else return total;

Any help would be very much appreciated.



Recommended Answers

All 5 Replies

Your problem is the subtraction of y. It doesn't do anything.

The point about recussion is that in a default case you
return a value e.g. if (y==1) return x; but in more general cases you return a simpler function .e.g if (y>1) return add(x,y-1); However, once you get that to work for postive x and y, note that with a very small change it will also work with negative x and y.
(and show and interesting demonstration as to why -3 * -4 =12. )

[tinfoil hat]It is not a proper mathematical proof[/tinfoilhat]

So if I don't decrement y, how do I get the recursion to stop? If I take out the y--, won't it just keep going forever?

It is that your decrement of y does absolutely nothing.

total start as???? Absolutely anything.

Your function is not recursive, it doesn't call itself. Imagen you call your function like this myMath(3,4); Let us step through and see what happens:

// This is the effect it is NOT code:
x=3; y=4                :     line ::        int myMath(int x, int y) 
total = ANYTHING         : line :: int total; 
Take branch           : line :: if ( y > 0) {
total=ANYTHING+x       : line ::total = total + x;
y=3;                       : line :: y--
// Compiler error here: reached a function and have not return.

y is LOCAL to the function only. if you call it again with myMath(10,7)
then y will be 7.

To be recursive you need to call myMath FROM inside myMath.

Thank you for all of your help so far. I thought I had it calling itself, but I must have changed it at some point.

So, take out the y--, and put in myMath(x,y-1), should that work? How do I get total to start out at 0 without continuing to be 0 all the time? I know if I set total to 0 at the beginning of the myMath function, it will reset it to 0 every time?

Thanks again,


//For positiver integers thats within range of int
int RecursiverMult( int x, int y)
    //if y is less than 1 then return 0
   //else return x plus RecursiveMult(x,y-1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.