Help with Recursion in C++

Started by
17 comments, last by NightCreature83 13 years, 1 month ago
Well I'm currently in the topic of function recursion, so i tried this function which supposedly displays 1 to 10 using a recursive function named recurse().

It runs, but nothing shows in the console window upon execution.

Here is the code




#include <iostream>
using namespace std;

int x;

void recurse();

int main()
{

x = 0;
recurse();

char end; // This is my ending sequence to prevent program from closing immediately
cin >> end;
cin.ignore(end);
return 0;
}

void recurse()
{

x = x + 1;
if(x = 11){
return;}
else{
cout << x << "\n";
recurse();
}

}





That's my code basically. I would appreciate to know what went wrong with my code. Thank you!
Advertisement
Hi,

You have a typo in your test to stop recursion

if(x = 11)


should be:


if(x == 11)


The first way will assign the value 11 to x and then evaluate x's value (which is 11, that is something different than 0 and evaluates to true; therefore that code path will always be taken.

Cheers!

--
Edit: typos
You have an assignment in your conditional. The line if(x = 11) sets x to 11, and then tests if 11 is non-zero. It is, so the function always returns immediately.
You might avoid the global declaration of the x variable. That would make you recursion function self contained, or a bit more readable.

I would re-write such function as this:

int recurse( int x )
{
cout << x << "\n";

if( x < 10 )
x = recurse( x + 1 );

return x;
}
I agree to that ericbeg said - Avoid globasllvariables as much as you can, pass local variable x to your function instead

Deltron Zero and Automator.


You might avoid the global declaration of the x variable. That would make you recursion function self contained, or a bit more readable.

I would re-write such function as this:

int recurse( int x )
{
cout << x << "\n";

if( x < 10 )
x = recurse( x + 1 );

return x;
}







void recurse(int x)
{
cout << x << "\n";
if( x < 10 )
recurse( x + 1 );
}



Would do the same thing and avoids an unnecessary return value, this is better unless you actually need the value outside of the recurse function.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This isn't a better solution, but another way to look at it :

void print1To10(){
_print(1,10);
}


void _print(const int begin, const int end){
if(begin <= end) cout << begin;
else _print(begin+1,end);
}
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
It is traditional not to post direct answers to homework questions.

It is traditional not to post direct answers to homework questions.


To be fair he figured the recursion out by himself, just a minor syntax error which is easy to mis when you are staring at it for a few hours. The rest are just improvements on what he had, you need to read quite a bit of code before you can actually write code, imo.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thanks! You guys helped a lot!

This topic is closed to new replies.

Advertisement