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