Check if an Array is Symmetric in C++

In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first element is equal to the last, the second is equal to the second last, and so on. This problem will help you practice array traversal and comparison techniques.

In this task, you will implement a function that checks if the array is symmetric by comparing the elements from both ends of the array. The function should return true if the array is symmetric and false otherwise. You will need to handle arrays of various sizes, including even and odd-length arrays.

Group

Arrays and Vectors in C++

Objective

1. Define an array and initialize it with some values.
2. Create a function that compares the elements of the array from both ends.
3. The function should return true if the array is symmetric and false otherwise.
4. Test the program with different arrays, including arrays with an even and odd number of elements.
5. Output the result indicating whether the array is symmetric or not.

Implement a function that determines if an array is symmetric.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to check if the array is symmetric
bool isSymmetric(int arr[], int size) {
    // Loop through half of the array
    for (int i = 0; i < size / 2; i++) {
        // Compare the element from the beginning with the corresponding element from the end
        if (arr[i] != arr[size - 1 - i]) {
            return false; // If a mismatch is found, the array is not symmetric
        }
    }
    return true; // If no mismatch is found, the array is symmetric
}

int main() {
    // Define an array with symmetric elements
    int arr[] = {1, 2, 3, 2, 1};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array

    // Call the function to check if the array is symmetric
    if (isSymmetric(arr, size)) {
        cout << "The array is symmetric." << endl; // Output if the array is symmetric
    } else {
        cout << "The array is not symmetric." << endl; // Output if the array is not symmetric
    }

    return 0; // End of program
}

 Output

The array is symmetric.

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Sort an Array of Numbers in Ascending Order in C++

    In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are se...

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...

  • Calculate the Average of Array Elements in C++

    In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, w...

  • Remove a Specific Element from an Array in C++

    In this exercise, you will develop a program that removes a specific element from an array. Removing elements from an array is a common task, especially when working with data that...

  • Find the Largest Number in an Array in C++

    In this exercise, you will implement a function that finds the largest number in an array. This task is useful when working with datasets where you need to extract the maximum valu...