HI I am trying to work on a very simple recursive function but i get o/p as some negative values.. can u please help and tell me what mistake i am doing ?

#include<iostream.h>


void countdown(int x)
{
using namespace std;
cout<<x<<endl;
countdown(x-1);
}

int main(void)
{
countdown(10);
return 0;
}

1) it goes negative because there is nothing to prevent it. Its an infinit recursive function -- that is until it consumes all available stack space and then it will simply crash. You need to add a line that stops the recursion when x == 0 (or whatever other value you want).

2) replace iostream.h with <iostream>. Current c++ standards do not use the .h extension. If you compiler doesn't support <iostream> then get a newer compiler.

3) move that line using namespace std; up above just under the last include directive. It does not good to place it where it is.

Hi AncientDragon:

Thanks for ur reply.. i made some modifications.. still does not work! i get all 0s now..

#include<iostream.h>

using namespace std;
void countdown(int x)

{
cout<<x<<endl;
while(x > 0)
{
countdown(x-1);
}
}

int main(void)
{
countdown(10);
return 0;
}
~                                                                                             
~                                                                                             
~

1) it goes negative because there is nothing to prevent it. Its an infinit recursive function -- that is until it consumes all available stack space and then it will simply crash. You need to add a line that stops the recursion when x == 0 (or whatever other value you want).

2) replace iostream.h with <iostream>. Current c++ standards do not use the .h extension. If you compiler doesn't support <iostream> then get a newer compiler.

3) move that line using namespace std; up above just under the last include directive. It does not good to place it where it is.

Hey THanks it worked.. but how does it matter.. "while" was supposed to be doing the same thing..as "if" is doing now.. then why did it falter ?


you don't want a while statement, but just a simple if statement

if( x > 0)
   countdown(x-1);

The while didn't work because now you have recursion within recursion. Follow the function through with pencil & paper and you will see why it doesn't work like you wanted it to.

I dont see the difference.. it is going to call the countdown function repetitively anyway..!

With the while statement, the function will call itself billions of times. Try this version and see the results

#include <iostream>

using namespace std;

long long counter = 0;
void countdown(int x)
{
    ++counter;
    if( counter % 10000000 == 0)
        cout << counter << "\n";
    //cout<<x<<endl;
    while(x > 0)
    {
        countdown(x-1);
    }
}

int main(void)
{
countdown(10);
cout << "counter = " << counter << "\n";
return 0;
}

Oh man!! Ancient Dragon would be snatching his hairs!!

Look kavithabaskar, You are basically using recursions to avoid loops.
Every series has Recursive form and a closed form(non-recursive)[Note that it is not possible for every series to have both forms].
Say, the series 1,2,3,4,5,6,7,8 has a recursive form as [TEX]A_{n}=A_{n-1}+1 || A_{1}=1[/TEX]
and open form as [TEX]A_{n}=n[/TEX]
So, if you know the closed form it is best to use it through loops.
But when you dont know closed form you go to recursive functions.

Comments
This article has been dead for over six months. Start a new discussion instead.