• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Bladelock

Help with Recursion in C++

18 posts in this topic

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

[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();
}

}

[/code]



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

Share this post


Link to post
Share on other sites
Hi,

You have a typo in your test to stop recursion
[code]
if(x = 11)[/code]

should be:

[code]
if(x == 11) [/code]

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
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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:

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

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

return x;
}[/code]
1

Share this post


Link to post
Share on other sites
[quote name='ericbeg' timestamp='1300196493' post='4786017']
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:

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

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

return x;
}[/code]
[/quote]




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


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.
1

Share this post


Link to post
Share on other sites
This isn't a better solution, but another way to look at it :
[code]
void print1To10(){
_print(1,10);
}


void _print(const int begin, const int end){
if(begin <= end) cout << begin;
else _print(begin+1,end);
}
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1300227396' post='4786229']
It is traditional not to post direct answers to homework questions.
[/quote]

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.
2

Share this post


Link to post
Share on other sites
[quote name='D.Chhetri' timestamp='1300224950' post='4786210']
This isn't a better solution, but another way to look at it :
[code]
void print1To10(){
_print(1,10);
}


void _print(const int begin, const int end){
if(begin <= end) cout << begin;
else _print(begin+1,end);
}
[/code]
[/quote]
this will print just 1.

I think you mean :
[code]
void print1To10(){
_print(1,10);
}


void _print(const int begin, const int end){
if(begin <= end) _print(begin+1,end);
cout << begin;
}
[/code]
0

Share this post


Link to post
Share on other sites
To avoid future mistakes put literal constants on left side so that you can caught this potentially nasty bug at compile time:
[code]
...
if( 11 = x )//error
if(11 == x) //fine and same as if(x == 11)
[/code]
1

Share this post


Link to post
Share on other sites
[quote name='belfegor' timestamp='1300391317' post='4787137']
To avoid future mistakes put literal constants on left side so that you can caught this potentially nasty bug at compile time:
[code]
...
if( 11 = x )//error
if(11 == x) //fine and same as if(x == 11)
[/code]
[/quote]

I've seen this kind of thing before, but I don't know if it really helps anything: If you can remember to put the number first, you can also remember to write "==" instead of "=".
0

Share this post


Link to post
Share on other sites
[quote name='sjaakiejj' timestamp='1300392999' post='4787147']
It does help, as doing that every time makes it a habbit, whilst missing out an "=" can often be due to a typo, and lead to an incredible headache when debugging.
[/quote]

Well, I made a habit of writing "==". I think it's been about 18 years since the last time I made that mistake.
0

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1300393237' post='4787149']
[quote name='sjaakiejj' timestamp='1300392999' post='4787147']
It does help, as doing that every time makes it a habbit, whilst missing out an "=" can often be due to a typo, and lead to an incredible headache when debugging.
[/quote]

Well, I made a habit of writing "==". I think it's been about 18 years since the last time I made that mistake.
[/quote]

if I'm writing a long set of conditionals it's pretty easy to slip and miss an =, or if some crumbs get in your keyboard. It's an easy thing to miss, and it's a pain to debug because when you look at it it's really easy to see '=' as '==' even though they are completely different.

Doesn't happen often, but when it does it's pretty annoying.
0

Share this post


Link to post
Share on other sites
Also, it's especially good practice to put the constant first if you will be sharing code. Consider the time back in 2003 when somebody inserted malicious code into a CVS clone of the main BitKeeper repository for the Linux kernel. In the wait4 function they added:

[code]if ((options == (__WCLONE|__WALL)) && (current->uid = 0))[/code]

A handy little backdoor to gain root privileges for any program you choose simply by calling wait4 and compiling with the right options. Maybe you and everybody with push access to your repository is sharp eyed enough to catch these every time, but requiring the constant on the left means you don't have to be.
0

Share this post


Link to post
Share on other sites
[quote name='lmelior' timestamp='1300405185' post='4787230']
Also, it's especially good practice to put the constant first if you will be sharing code. Consider the time back in 2003 when somebody inserted malicious code into a CVS clone of the main BitKeeper repository for the Linux kernel. In the wait4 function they added:

[code]if ((options == (__WCLONE|__WALL)) && (current->uid = 0))[/code]

A handy little backdoor to gain root privileges for any program you choose simply by calling wait4 and compiling with the right options. Maybe you and everybody with push access to your repository is sharp eyed enough to catch these every time, but requiring the constant on the left means you don't have to be.
[/quote]

I don't think it should be required in any project, it should however be a guideline when writing code, so more of a preferred style than be strictly enforced. This makes less people balk at restrictive coding style rules, which leads to happier developers.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0