Checking an array if it's palindrome or not, using C++


Checking an array if it's palindrome or not, using C++


Hi, this is Mazen. Welcome to a new blog and new example. In this blog, we're going to solve a programming question using C++. Each step is explained before coded, so you can change it to a code using whatever programming language you would prefer. We're continuing our series of solving examples on arrays. Today's example is checking whether an entered array is palindrome or not. Simply, a palindrome number - or a text in general - is a set of digits that are the same either read from left to right or right to left. Lets take an example: 24742 is a palindrome number because when you read it either from the right or the left, it won't make difference. On contrast, 83525 is not a palindrome number, because when you read it from the right it would be 52538, which is definitely not the same when reading it from the left.

Write a program to check whether the elements of an array are the same either read from left to right or right to left (palindrome), or not

Step 1: Declare an array with the size of 20, for instance.

    int array[20];

Step 2: Prompt the user to enter the count of digits of the number he's going to enter.

    std::cout << "How many elements the array shall has? ";
    int noOfElements;
    std::cin >> noOfElements;

Step 3: Prompt the user to fill the elements of the array with the count he chose. Use a for loop to pass through the elements of the array. The initial value is -  as usual - zero. The condition is that the initial value shouldn't exceed the number of elements the user previously specified (noOfElements). The initial value is incremented each repetition by one.


    for (int i = 0; i < noOfElements; i++)
    {
        std::cin >> array[i];
    }

Step 4: Check whether the array elements are palindrome or not using the following logic:
1) Create a bool variable as a flag, name it "isPalindrome", and give it an initial value of "true".
2) Create a for loop to pass through the indexes of the array, with properties just like the previously declared one.
3) Inside of the body of the loop, compare each element to its analogue at the end, if the number of elements is 10, the analogue of the first element is the tenth one in the other array, the analogue of the second element is the ninth one, and so on.
We can reach the index of the last element by using the following equation: ((noOfElements - 1) - i) where i is the current value of the loop and noOfElements is the number of elements the user entered.
4) The comparison is achieved using an if statement. If an element is not equal to its analogue, then "false" is assigned to "isPalindrome" and the program breaks out of the loop. Otherwise, the loop will continue working until i hits noOfElements.


    bool isPalindrome = true;
    for (int i = 0; i < noOfElements; i++)
    {
        if (array[i] != array[(noOfElements - 1) - i])
{
    isPalindrome = false;
    break;
}
    }

Step 5: Finally, the program determines whether the entered numbers are palindrome or not, using an if statement placed after the body of the for loop. The condition is "isPalindrome", if true, the program tells the user that the entered numbers are palindrome. Otherwise, the user is told that the numbers he entered are not palindrome.


    if (isPalindrome)
        std::cout << 
    " are the same either read from left to right or right to left (palindrome)\n";
    else
        std::cout << 
    " are not the same either read from left to right or right to left (non                        palindrome)\n";


Putting it all together:

    	#include <iostream>

        int main()
        {
            int array[20];

            std::cout << "How many elements the array shall has? ";
            int noOfElements;
            std::cin >> noOfElements;

            std::cout << "Enter values for each of the "
                << noOfElements << " elements of the array:\n";
            for (int i = 0; i < noOfElements; i++)
            {
                std::cout << "Integer #" << i + 1 << ": ";
                std::cin >> array[i];
            }


            bool isPalindrome = true;
            for (int i = 0; i < noOfElements; i++)
            {
                if (array[i] != array[(noOfElements - 1) - i])
                {
                    isPalindrome = false;
                    break;
                }
            }

            std::cout << std::endl;

            for (int i = 0; i < noOfElements; i++)
                std::cout << array[i] << ' ';

            if (isPalindrome)
                std::cout << 
                    " are the same either read from left to right or right to left (palindrome)\n";
            else
                std::cout << 
                    " are not the same either read from left to right or right to left (non palindrome)\n";

            system("pause");
            return 0;
        }
    

Output:

Checking an array if it's palindrome or not, using C++


Comments

Popular posts from this blog

Finding (positive - negative - odd - even - zero) numbers in an array

Two methods for reversing a text string in c++

Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++