Recursion

C

case.learning

Hi everyone,

I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.

bool IsPalindrome(string s, int first, int last)
{
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}

Note: the function returns false all the time, even though the last
call of recursion return true.

Thanks again.

Mark.
 
R

Robert Bauck Hamar

Hi everyone,

I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.

bool IsPalindrome(string s, int first, int last)
{
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}

Note: the function returns false all the time, even though the last
call of recursion return true.

If you read the compiler warnings, it'll probably tell you what's wrong: You
forgot the return statement.
 
C

case.learning

Hi everyone,
I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.
bool IsPalindrome(string s, int first, int last)
{
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}
Note: the function returns false all the time, even though the last
call of recursion return true.

If you read the compiler warnings, it'll probably tell you what's wrong: You
forgot the return statement.

Oh right. Thanks very much. I used gdb, and it didn't tell me
anything.

Thanks again.

Mark.
 
M

Markus Schoder

Hi everyone,
I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.
bool IsPalindrome(string s, int first, int last) {
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}
Note: the function returns false all the time, even though the last
call of recursion return true.

If you read the compiler warnings, it'll probably tell you what's
wrong: You forgot the return statement.

Oh right. Thanks very much. I used gdb, and it didn't tell me anything.

The compiler will tell you not the debugger. If you are using gcc/g++ try
the -Wall switch.
 
C

case.learning

(e-mail address removed) wrote:
Hi everyone,
I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.
bool IsPalindrome(string s, int first, int last) {
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}
Note: the function returns false all the time, even though the last
call of recursion return true.
If you read the compiler warnings, it'll probably tell you what's
wrong: You forgot the return statement.
Oh right. Thanks very much. I used gdb, and it didn't tell me anything.

The compiler will tell you not the debugger. If you are using gcc/g++ try
the -Wall switch.

I actually compiled it with g++ 4.1 and it didn't issue any messages.
I didn't use the -Wall switch though. I'll try.

Mark.
 
R

Robert Bauck Hamar

(e-mail address removed) wrote:
Hi everyone,
I'm a C++ novice. I'm trying to learn recursion and have written
this piece of code but it doesn't work. Could you help me to see
where it goes wrong? Thanks.
bool IsPalindrome(string s, int first, int last) {
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}
Note: the function returns false all the time, even though the last
call of recursion return true.
If you read the compiler warnings, it'll probably tell you what's
wrong: You forgot the return statement.
Oh right. Thanks very much. I used gdb, and it didn't tell me anything.

The compiler will tell you not the debugger. If you are using gcc/g++ try
the -Wall switch.

I actually compiled it with g++ 4.1 and it didn't issue any messages.
I didn't use the -Wall switch though. I'll try.

With g++, I suggest you use the switches -ansi, -pedantic, and -Wall. They
will tell you about errors you make, both errors that g++ handles as
extensions, and probable errors.
 
J

Jon Harrop

I'm a C++ novice. I'm trying to learn recursion and have written this
piece of code but it doesn't work. Could you help me to see where it
goes wrong? Thanks.

bool IsPalindrome(string s, int first, int last)
{
if(last <= first)
return true;
else if(s[first] != s[last])
return false;
else
IsPalindrome(s, first+1, last-1);
}

Note: the function returns false all the time, even though the last
call of recursion return true.

Interesting that you are writing in a purely functional style. You might
like to ossify that by littering the C++ code with "const".

Also, you can combine your boolean expressions more effectively, e.g. in
OCaml:

let rec is_palindrome i j s =
i>=j || s. = s.[j] && is_palindrome (i+1) (j-1) s

For example:

# List.map (is_palindrome 0 3) ["boob"; "poop"; "****"];;
- : bool list = [true; true; false]

You can even partially implement this in C++:

bool is_palindrome(const int i, const int j, const std::string s) {
return i>=j || s == s[j] && is_palindrome(i+1, j-1, s);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top