C++ Program to Compare Two Arrays

Write C++ Program to Compare Two Arrays

// CPP Program to Compare Two Arrays

#include <iostream>

using namespace std;

int main()
{
	int A[] = {8, 7, 6, 5, 2, 3, 4, 1};
	int B[] = {8, 7, 6, 5, 2, 3, 4, 1};

	int i, j, flag = 0;

	int l1 = sizeof(A)/sizeof(A[0]);
	int l2 = sizeof(B)/sizeof(B[0]);

        cout << "Array A :--> ";
	for(i=0; i<l1; i++)
        cout << A[i] << " ";

        cout << "\nArray B :--> ";
	for(i=0; i<l2; i++)
        cout << B[i] << " ";

	if(l1 != l2)
	{
		flag = 1;
	}
	else
	{
		for(i = 0;i < l1; i++)
		{
                    if(A[i] != B[i])
                    {
                        flag = 1;
			break;
                    }
		}
	}

	if(flag == 0)
	{
		cout << "\n\nArrays are equal";
	}
	else
	{
		cout << "\n\nArrays are not equal";
	}
	return 0;
}

Output:

Run 1:
Array A :--> 8  7  6  5  2  3  4  1
Array B :--> 1  2  3  4  5  6  7  8

Arrays are not equal

Run 2:
Array A :--> 8  7  6  5  2  3  4  1
Array B :--> 8  7  6  5  2  3  4  1

Arrays are equal

≪ Previous | Next ≫