C++ palindrome debugging

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

There are a few problems to your edited response:

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

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

  • 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!


    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?

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

    链接地址: http://www.djcxy.com/p/86324.html

    上一篇: C ++头文件和编译过程

    下一篇: C ++回文调试