myProgrammingLab "palindrome" challenge and recursion

2.5k Views Asked by At

I'm taking an Intro to Programming class and a good chunk of the material is drilled into our heads through myProgrammingLab. I'm having a little trouble with the concept of Recursion... It's sort of been hit or miss for me. This particular problem has me stumped. When I submit my code, it offers me

CTest1.cpp: In function 'bool isPalindrome(int*, int)':
CTest1.cpp:9: error: invalid conversion from 'int' to 'int*'
CTest1.cpp:9: error:   initializing argument 1 of 'bool isPalindrome(int*, int)'"

as advice, which I can assure you is not very helpful. Lol

I think my main problem is when I get to the actual recursion. I'm aware that something's off, but.. If you could just point me in the right direction, I would very much appreciate it.

A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward)

Write a recursive, bool-valued function, isPalindrome, that accepts an integer -valued array , and the number of elements and returns whether the array is a palindrome.

An array is a palindrome if: the array is empty (0 elements ) or contains only one element (which therefore is the same when reversed), or the first and last elements of the array are the same, and the rest of the array (i.e., the second through next-to-last elements ) form a palindrome.

My code so far:

bool isPalindrome(int arr[], int n){
    if (n == 0 || n == 1)
    {
        return true;
    }
    else if (arr[n-1] == isPalindrome(arr[((n-1) - n) +1 ], n))
    {
        return true;
    } 
    else {
        return false;
    }
}

EDIT:

I have now changed my problem line to

else if (arr[n-1] == isPalindrome(arr[], n-1))

Now I get the hint "We think you might want to consider using +." I can't see where adding would apply here. I don't think it's talking about incrementing because it references incrementing as "++" like in code.

I am also given the error message:

CTest1.cpp: In function 'bool isPalindrome(int*, int)': CTest1.cpp:9: error: expected primary-expression before ']' token

Is the second line giving an error because the brackets are empty in my first argument?

2

There are 2 best solutions below

0
On

When you recursively call isPalindrome you pass these two arguments

isPalindrome(arr[((n-1) - n) +1], n)

The second is fine, but the first argument evaluates to

arr[index]

Which is just indexing a value out of your array, and will evaluate to int. Since it is looking for an int* or in other words, the address of the element to start at, you can pass

&arr[((n-1) - n) +1]
0
On

The compiler advice is actually very helpful for you in this situation.

CTest1.cpp:9: error: invalid conversion from 'int' to 'int*'

It is saying that you are giving the function an int, whereas the function is expecting an int*.

CTest1.cpp:9: error:   initializing argument 1

Is saying that the problem is in argument 1.

Now to your code. See this line:

else if (arr[n-1] == isPalindrome(arr[((n-1) - n) +1 ], n))

You can see that the first argument you are sending is not an int* (i.e. not the pointer to the array), but an element of the array. You are sending an int as the first argument.

Try to rework your code taking this into account.