Problem Statement
Take 2 arrays and then check if both the arrays are equal or not. An array is equal if the size and elements are same regardless of their position.
Concepts and Explanation
This program checks whether two arrays are equal regardless of their position.
Header files: #include <iostream>
The iostream
header file is part of the C++ standard library, providing functionality for input and output streams. In this program, we use this header file.
Main Function
The main()
function is the entry point of the program, where the code execution starts. It has a return type of int
.
Variables
We declare two integer arrays of equal size named array1
and array2
, and a boolean array named checked
to keep track of which elements have already been compared.
Console Input and Output
We use cout
to print messages to the console, asking the user to enter the values for both arrays. We use cin
to read in the values entered by the user and store them in the respective arrays.
Checking for Equality
We use nested loops to iterate over every element of both arrays. For each element in array1
, we compare it to each element in array2
. If a matching element is found in array2
and the corresponding element in checked
is still false
, we mark the element in checked
as true
and move to the next element in array1
. If an element in array2
does not match the element in array1
, we continue iterating through array2
.
If all elements in array1
have a matching element in array2
and the corresponding elements in checked
are all true
, then the two arrays are equal, regardless of their position. Otherwise, the two arrays are not equal.
Outputting the Result
We use cout
to output the result of the comparison to the user.
Ending the Program
The program ends by returning 0
, indicating that the program has executed successfully.
Method 1: Without using any external library
Code
/*
* -----------------------------------------------------------
* OOPS using C++ Language (CSU1287P)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <iostream>
using namespace std;
bool isEqual(int arr1[], int arr2[], int size1, int size2) {
if(size1 != size2) {
return false;
}
bool found;
for(int i = 0; i < size1; i++) {
found = false;
for(int j = 0; j < size2; j++) {
if(arr1[i] == arr2[j]) {
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
}
int main() {
int size1, size2;
cout << "Enter the size of array 1: ";
cin >> size1;
int arr1[size1];
cout << "Enter the elements of array 1: ";
for(int i = 0; i < size1; i++) {
cin >> arr1[i];
}
cout << "Enter the size of array 2: ";
cin >> size2;
int arr2[size2];
cout << "Enter the elements of array 2: ";
for(int i = 0; i < size2; i++) {
cin >> arr2[i];
}
if(isEqual(arr1, arr2, size1, size2)) {
cout << "Arrays are equal" << endl;
} else {
cout << "Arrays are not equal" << endl;
}
return 0;
}
Method 2: Using vector
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool isEqual(int arr1[], int arr2[], int size1, int size2) {
if(size1 != size2) {
return false;
}
vector v1(arr1, arr1 + size1);
vector v2(arr2, arr2 + size2);
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
return (v1 == v2);
}
int main() {
int size1, size2;
cout << "Enter the size of array 1: ";
cin >> size1;
int arr1[size1];
cout << "Enter the elements of array 1: ";
for(int i = 0; i < size1; i++) {
cin >> arr1[i];
}
cout << "Enter the size of array 2: ";
cin >> size2;
int arr2[size2];
cout << "Enter the elements of array 2: ";
for(int i = 0; i < size2; i++) {
cin >> arr2[i];
}
if(isEqual(arr1, arr2, size1, size2)) {
cout << "Arrays are equal" << endl;
} else {
cout << "Arrays are not equal" << endl;
}
return 0;
}
Method 3: Shortest Possible Implementation
#include <iostream>
#include <algorithm>
int main() {
int a[100], b[100], n;
std::cout << "Enter the size of array and press enter: ";
std::cin >> n;
std::cout << "Now enter the elements of first array. Seperate them with spaces - (Example: 1 4 2 3): ";
for (int i = 0; i < n; i++) std::cin >> a[i];
std::cout << "Now enter the elements of second array. Seperate them with spaces - (Example: 1 4 2 3): ";
for (int i = 0; i < n; i++) std::cin >> b[i];
std::sort(a, a + n);
std::sort(b, b + n);
std::cout << (std::equal(a, a + n, b) ? "Arrays are Equal" : "Arrays are Not Equal") << std::endl;
return 0;
}
Method 4
#include <iostream>
#include <algorithm>
bool arraysEqual(int a[], int b[], int n) {
std::sort(a, a + n);
std::sort(b, b + n);
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
int main() {
int a[100], b[100], n;
std::cout << "Enter the size of array and press enter: ";
std::cin >> n;
std::cout << "Now enter the elements of first array. Seperate them with spaces - (Example: 1 4 2 3): ";
for (int i = 0; i < n; i++) std::cin >> a[i];
std::cout << "Now enter the elements of second array. Seperate them with spaces - (Example: 1 4 2 3): ";
for (int i = 0; i < n; i++) std::cin >> b[i];
if (arraysEqual(a, b, n)) {
std::cout << "Arrays are Equal" << std::endl;
} else {
std::cout << "Arrays are Not Equal" << std::endl;
}
return 0;
}
Output for Method 1:
This program checks if the entered array are equal regardless of their position, using object-oriented programming concepts and algorithms in C++.
Enter the size of array 1: 3
Enter the elements of array 1: 1 4 2
Enter the size of array 2: 3
Enter the elements of array 2: 4 1 2
Arrays are Equal