C++ Program to Check if Two Arrays Are Equal or Not

12/05/2024 0 By indiafreenotes

To determine if two arrays are equal, you need to check if they have the same length and if each corresponding element in the two arrays is the same. Below, I’ll provide a C++ program that compares two arrays using a function to handle this comparison. The program will be demonstrated with simple fixed-size arrays. For dynamic arrays or vectors, you might want to use std::vector for simplicity and flexibility, but here we’ll stick with basic array types to keep it focused on fundamental concepts.

Program Overview

The program will:

  1. Define two arrays.
  2. Compare these arrays using a function.
  3. Print out whether the arrays are equal or not.

Sample C++ Code

#include <iostream>

// Function to check if two arrays are equal

bool areEqual(int arr1[], int arr2[], int n1, int n2) {

    // If lengths of array are not equal means array are not equal

    if (n1 != n2)

        return false;

    // Linearly compare elements

    for (int i = 0; i < n1; i++)

        if (arr1[i] != arr2[i])

            return false;

    // If all elements were same.

    return true;

}

int main() {

    int arr1[] = {1, 2, 3, 4, 5};

    int arr2[] = {1, 2, 3, 4, 5};

    int arr3[] = {1, 2, 3, 4, 5, 6};

    int arr4[] = {1, 2, 3, 4, 6};

    int n1 = sizeof(arr1) / sizeof(arr1[0]);

    int n2 = sizeof(arr2) / sizeof(arr2[0]);

    int n3 = sizeof(arr3) / sizeof(arr3[0]);

    int n4 = sizeof(arr4) / sizeof(arr4[0]);

    if (areEqual(arr1, arr2, n1, n2))

        std::cout << “arr1 and arr2 are equal.” << std::endl;

    else

        std::cout << “arr1 and arr2 are not equal.” << std::endl;

    if (areEqual(arr1, arr3, n1, n3))

        std::cout << “arr1 and arr3 are equal.” << std::endl;

    else

        std::cout << “arr1 and arr3 are not equal.” << std::endl;

    if (areEqual(arr1, arr4, n1, n4))

        std::cout << “arr1 and arr4 are equal.” << std::endl;

    else

        std::cout << “arr1 and arr4 are not equal.” << std::endl;

    return 0;

}

Code Explanation

  • Function are Equal:

This function accepts two integer arrays along with their sizes. It first checks if their sizes are equal. If not, it immediately returns false. If the sizes are the same, it then compares each corresponding element in the arrays. If any pair of elements differ, it returns false. If it completes the loop without finding any differences, it returns true.

  • Main Function:

Defines a few arrays and their sizes, then uses the areEqual function to compare them, printing out the results of these comparisons.

This approach efficiently checks array equality by focusing first on length and then on individual element comparison. This is a general method that can be adapted for other data types and more complex data structures by modifying the comparison criteria and handling more complex element types (like strings or custom objects). For real-world applications, especially where dynamic sizing or more complex comparisons are needed, consider using std::vector or other suitable data structures from the C++ Standard Library.