Help with Recursion in C++

Started by
17 comments, last by NightCreature83 13 years, 1 month ago

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


this will print just 1.

I think you mean :

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


void _print(const int begin, const int end){
if(begin <= end) _print(begin+1,end);
cout << begin;
}
Advertisement
To avoid future mistakes put literal constants on left side so that you can caught this potentially nasty bug at compile time:

...
if( 11 = x )//error
if(11 == x) //fine and same as if(x == 11)

To avoid future mistakes put literal constants on left side so that you can caught this potentially nasty bug at compile time:

...
if( 11 = x )//error
if(11 == x) //fine and same as if(x == 11)



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 "=".

[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:

...
if( 11 = x )//error
if(11 == x) //fine and same as if(x == 11)



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 "=".
[/quote]

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.

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.


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

I think it's been about 18 years since the last time I made that mistake
[/quote]
Good for you.

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


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

if ((options == (__WCLONE|__WALL)) && (current->uid = 0))

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.

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:

if ((options == (__WCLONE|__WALL)) && (current->uid = 0))

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.


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.

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

This topic is closed to new replies.

Advertisement