-2

I came across this question in my notes: Given an array of integers, write a function to check if the elements in that array are palindromes.I've been working on my code and it looks something like this:

#include<iostream>
#include<cmath>
using namespace std;

bool is_a_palindrome(int integers[], int length){
    int i;
    int middle = floor(length / 2);

    //while (length != 0 && length > 0){
    for (i = 0; i < middle; i++){

        if (integers[i] != integers[length - 1 -i]){
            return -2;
        }
        else{
            return true;
        }
    }
}

int main(){

    int array[4] = {1,2,2,1};
    int length = 4;
    is_a_palindrome(array, length);
}

When I run the code, I'm expecting to get either 1 for being true or -2 for it being false. At the moment, I'm not getting anything. I'm not too sure where the problem is. Any help is appreciated.

fixed code according to the comments:

#include<iostream>
#include<cmath>
using namespace std;

bool is_a_palindrome(int integers[], int length){
    int i;
    int middle = floor(length / 2);

    //while (length != 0 && length > 0){
    for (i = 0; i < middle; i++){

        if (integers[i] == integers[length - 1 -i]){
            return true;
        }
        else{
            return false;
        }
    }
}

int main(){

    int array[4] = {1,2,2,1};
    int length = 4;
    return is_a_palindrome(array, length);
}
  • 6
    Please post your actual code, not a screenshot. – Cory Kramer Aug 06 '15 at 12:20
  • 1
    Click [edit], copy-paste your code in place of the image link, select the code portion of your question, and click the `[{ }]` button at the top of the editor. Click [save]. – Sergey Kalinichenko Aug 06 '15 at 12:22
  • this reminds me of [Why is this program erroneously rejected by three C++ compilers?](http://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers) – m.s. Aug 06 '15 at 12:23
  • 1
    you might want to add `return` before function call, so program will return value of function to OS. – Hcorg Aug 06 '15 at 12:23

3 Answers3

2

Some comments (for when you have fixed the markup of your question):

  • You are expecting output, but are you actually printing something?
  • main expects that you return and int, but you are not returning anything
  • Why are you returning -2 instead of false? The return type is boolean, so it cannot store the integer value.
  • Think about where to put the return true. When is something a palindrome?
Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
  • 1
    Just to note if there's no return in `main` it'll automatically return 0. – AliciaBytes Aug 06 '15 at 12:33
  • Thanks, I actually wasn't sure if this was true for every compiler. – Ben Ruijl Aug 06 '15 at 12:33
  • 1
    @BenRuijl it is called out in the standard 3.6.1.5 `...If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;` – NathanOliver Aug 06 '15 at 12:45
  • @BenRuijl I've tried fixing the code. And when I use the return command, isn't it supposed to still give me either ````1```` or ````0````? – paurush_008 Aug 06 '15 at 12:58
  • Yes, that part is correct now. However there is still a problem: you only know that a palindrome is actually a palindrome when you have checked _every_ character of the first half of the string. You are making a decision based on the first character. – Ben Ruijl Aug 06 '15 at 13:51
2

There are a few problems to your edited response:

  1. You return true as soon as a value in the front is the same as a value in the back. If you had an array {1,2,3,5,6,8,7,8,9,1} your function would return true because there is a 1 in the front and back of the array.

  2. You return -2 instead of false in a function that returns a boolean.

  3. Main returns an int and you return a call to your helper function which returns a boolean.

What you could do is use a vector instead of an array and use the std::reverse function to check if the vectors are the same. Or you could instead fix your for loop to not return early, but rather check if the value is ever not the same in the "front and back" and return true if you get through the entire for loop. Something like this:

`    for (i = 0; i < middle; i++)
     {
        if (integers[i] != integers[length - 1 -i])
            return false;
     }
     return true;
`

The rest is up for you to fix, but I think multiple people have already identified those problems for you. Good luck!

Jon
  • 150
  • 1
  • 3
  • 15
-2

You forgot the return before the calling to your palindrome function.

int main() {
    int array[4] = {1,2,2,1};
    into length = 4;

    return yourPalindromeFunction( array, length );
}

Because you did not write anything, C++ creates a default return that it gets zero.

blashser
  • 921
  • 6
  • 13
  • `main()` does not need to have a return. The C++11 standard 3.6.1.5 `...If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;` – NathanOliver Aug 06 '15 at 12:47
  • Of course, and this is the problem. It gets `0` instead of `true` (1) or `-2`. However, I edit to explain better. – blashser Aug 06 '15 at 13:23