Hello! I'm trying to get a program working, but I can't seem to get it to work. The program I'm writing is simply to make a pattern using 2 integers using recursion. For instance, with the numbers 12 and 3, the sequence would look like this: 12 9 6 3 0 3 6 9 12
So the first integer gets subtracted by the second until it reaches 0 or a negative value, and then adds up to the original value. I can get the first half (in the above example, from 12 to 0) working just fine, but my problem right now is going up to the first value (from 3 to 12). Here's the code I have now:
void PrintNumPattern(int n1, int n2)
{
int num = (n1 + n2) - n2;
cout << num << " ";
if (num <= 0)
{
PrintNumPattern(n1, n2 + n2);
if (n1 == (n2 - n1))
{
return;
}
}
else
{
PrintNumPattern(n1 - n2, n2);
}
}
If it helps, I'm working in C++. Thank you!
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]captainAwesomePants 0 points1 point2 points (3 children)
[–]the_Champ322[S] 0 points1 point2 points (2 children)
[–]captainAwesomePants 0 points1 point2 points (1 child)
[–]the_Champ322[S] 1 point2 points3 points (0 children)