-1

i made this program to make a function to check if integar array is palindrome or not.it is always giving output that it is not a palidrome on every input. can you plz help me out? code:

#include<iostream>
using namespace std;
void  palindrome(int[],int[],int);
int main()
{
    int size=5;
    int array1[size];
    int array2[size];
    palindrome(array1,array2,size);
    
}
void  palindrome(int array1[],int array2[],int size)
{
    size=5;
    int l=0;
    cout<<"enter your array=";
    for(int i=0;i<size;i++)
    {
        cin>>array1[i];
        
    }
    for(int i=0;i<size;i++)
    {
        array2[i]=array1[size-i];
    }
    
    
    if(array1==array2)
    {
        cout<<"given array is palindrome.";
    }
    else{
        cout<<"given array is not palindrome.";
    }

    
}
  • Use `std::vector` instead of raw arrays, and it will work. Here, you are comparing array adresses, not their contents. – Damien May 25 '21 at 08:24
  • 1. You can't meaningfully compare arrays with `==`, 2. The arguments are pointers, not arrays. Read more about arrays in your favourite C++ book. – molbdnilo May 25 '21 at 08:25

1 Answers1

1

You can't compare two arrays with ==, it will only compare if the array's address are equals, so different arrays never equals, we can switch it to std::equal

There is one memory issue with array2[i]=array1[size-i];, you will get a buffer overflow.

This is a slighly modified version of your code:

#include <iostream>
using namespace std;
void palindrome(int[], int[], int);
int main() {
  int size = 5;
  int array1[size];
  int array2[size];
  palindrome(array1, array2, size);
}
void palindrome(int array1[], int array2[], int size) {
  size = 5;
  int l = 0;
  cout << "enter your array=";
  for (int i = 0; i < size; i++) {
    cin >> array1[i];
  }
  for (int i = 0; i < size; i++) {
    array2[i] = array1[size - i - 1];
  }

  if (std::equal(array1, array1 + size, array2, array2 + size)) {
    cout << "given array is palindrome.";
  } else {
    cout << "given array is not palindrome.";
  }
}

It's recommended to use std::vector instead of the C style array:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
  int size = 5;
  palindrome(size);
}
void palindrome(int size) {
  std::vector<int> vec(size);
  cout << "enter your array=";
  for (int i = 0; i < size; i++) {
    cin >> vec[i];
  }
  std::vector<int> rev_rec{vec.rbegin(), vec.rend()};

  if (vec == rev_rec) {
    cout << "given array is palindrome." << std::endl;
  } else {
    cout << "given array is not palindrome." << std::endl;
  }
}

And we can also avoid the copy of vector, with reverse iterator:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
  int size = 5;
  palindrome(size);
}
void palindrome(int size) {
  std::vector<int> vec(size);
  cout << "enter your array=";
  for (int i = 0; i < size; i++) {
    cin >> vec[i];
  }
  if (std::equal(vec.begin(), vec.end(), vec.rbegin(), vec.rend())) {
    cout << "given array is palindrome." << std::endl;
  } else {
    cout << "given array is not palindrome." << std::endl;
  }
}
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42