Hi all.. This function should work recursively.. Meaning call the function inside the same function.

So i'm supposed to write a recursive function that takes a single parameter from type integer and decrements that number until it reaches zero.

Ex. If the user entered 7, he'll get this output:
7 6 5 4 3 2 1 0

This is my code.. But it is not working :s It keep returning the same number to me no matter what changes make.. :s
Would you check it please?

#include <iostream>
using namespace std;

int decrement (int n);

int main ()
{
	int number;

	cout<<"Enter a number to decrement it."<<endl;
	cin>> number;
	decrement (number);


return 0;
}

int decrement (int n)
{
if (n>=1)
return n;
else
{
decrement(n-1);
return n;
}
}

Recommended Answers

All 13 Replies

Your base case is wrong. When should your function return or Stop ?

How do i determine the right base case?
I mean here I want it to display 7 (n) as the first number. But then from the stack, 7 (n) is the last number saved.
I actually doubted my base case too..

The base case is when you want the recursion to stop going deeper and return back up the chain. In your case it's probably when n is less than zero.

Still facing the same problem.
The output is not returning anything ! I just enter the number then I get press any key to continue...
No matter what the base case is. :S
Anyway I've altered the base case as you advised.. and still... :(

int decrement (int n)
{
if (n<0)
return n;
else
{
decrement(--n);
return n;
}
}

Now what?

You are not getting any output because all the programs you have posted are calling decrement() but never displaying the value it returns. If you can output within your recursive function itself it will make things easier on you.

Still facing the same problem.
The output is not returning anything ! I just enter the number then I get press any key to continue...
No matter what the base case is. :S
Anyway I've altered the base case as you advised.. and still... :(

int decrement (int n)
{
if (n<0)
return n;
else
{
decrement(--n);
return n;
}
}

Now what?

You have no output statements. You will need to insert an output statement such as cout << n << " "; somewhere inside the structure of your if...else.

Thanks guys.. But now I've a nother problem...
I'm getting all negative numbers. All are irrational. I can't get why is that happening!
So I did as you suggested, i've inserted cout instead of return and comes the negative values. :S

int decrement (int n)
{
if (n<0)
return n;
else
{

decrement(--n);
cout<<n;

}
}

This is when it becomes time to sit down and write it out with pencil and paper. There are only a few numbers to deal with so it's manageable in this case. Write down what's happening at each call to your recursive function.
EDIT: goofed up base case

OH OH OH GUYS !! I DID IT !!! :D :D
I know I go crazy when get something right lol.
But only one problem .. it is incrementing, not decrementing :S
How do i make it decrement..?
Here's the last code..

int decrement (int n)
{
if (n<0)
return n;
else

decrement(n-1);

cout<<n;
}

Try putting your cout before your recursive call. It's probably functioning correctly but displaying them in the wrong order because you're displaying the deeper, rather than shallower, layers first

Try this:

int decrement (int n)
{
	if (n<0)
                return n;
	cout<<n;
	decrement(n-1);
	return n;
	
}
commented: Amazing help! He rocks ! +0

It is decrementing. Look carefully at the order of your statements, you only display the number after you have called the function, so the deepest function will output first. Try swapping the decrement statement and the cout statement.
Whoops, I missed the second page.

Awww guys really thanks for your help...
Pac-man I wouldn't stop thanking you ! I'd have never thought of doing that !!!!! I've never imagined that the code can be written without an else!! All of the ones that I prevously solves were with an else..
Really thanks !
xoxo

Be a part of the DaniWeb community

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