Problem statement:- Program to Check if two arrays are equal or not.
Data requirement:-
Input Data:- arr,size, arr2, size2
Output Data:-String output
Output Data:-String output
Additional Data:- i, j, temp
Program in C
Here is the source code of the C Program to Check if two arrays are equal or not.
Code:
#include<stdio.h>
int sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
}
main()
{
printf("Enter the size of the 1st array:");
int size,i;
scanf("%d",&size);
int arr[size];
printf("Enter the size of the 2nd array:");
int size2;
scanf("%d",&size2);
int arr2[size2];
printf("Enter the Element of the 1st array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the Element of the 2nd array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr2[i]);
}
if(check_equals(arr,arr2,size,size)==0)
printf("Not same....");
else
printf("same....");
}
Input/Output:int sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
}
main()
{
printf("Enter the size of the 1st array:");
int size,i;
scanf("%d",&size);
int arr[size];
printf("Enter the size of the 2nd array:");
int size2;
scanf("%d",&size2);
int arr2[size2];
printf("Enter the Element of the 1st array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the Element of the 2nd array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr2[i]);
}
if(check_equals(arr,arr2,size,size)==0)
printf("Not same....");
else
printf("same....");
}
Enter the size of the 1st array:5
Enter the size of the 2nd array:5
Enter the Element of the 1st array:
1
2
3
4
5
Enter the Element of the 2nd array:
1
2
3
4
5
same....
Enter the size of the 2nd array:5
Enter the Element of the 1st array:
1
2
3
4
5
Enter the Element of the 2nd array:
1
2
3
4
5
same....
Program in C++
Here is the source code of the C++ Program to Check if two arrays are equal or not.
Code:
#include<iostream>
using namespace std;
int sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
}
main()
{
cout<<"Enter the size of the 1st array:";
int size,i;
cin>>size;
int arr[size];
cout<<"Enter the size of the 2nd array:";
int size2;
cin>>size2;
int arr2[size2];
cout<<"Enter the Element of the 1st array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"Enter the Element of the 2nd array:\n";
for(i=0;i<size;i++)
{
cin>>arr2[i];
}
if(check_equals(arr,arr2,size,size)==0)
cout<<"Not same....";
else
cout<<"same....";
}
using namespace std;
int sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
}
main()
{
cout<<"Enter the size of the 1st array:";
int size,i;
cin>>size;
int arr[size];
cout<<"Enter the size of the 2nd array:";
int size2;
cin>>size2;
int arr2[size2];
cout<<"Enter the Element of the 1st array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"Enter the Element of the 2nd array:\n";
for(i=0;i<size;i++)
{
cin>>arr2[i];
}
if(check_equals(arr,arr2,size,size)==0)
cout<<"Not same....";
else
cout<<"same....";
}
Enter the size of the 1st array:4
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
5
4
3
8
Enter the Element of the 2nd array:
4
6
2
1
Not same....
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
5
4
3
8
Enter the Element of the 2nd array:
4
6
2
1
Not same....
Program in Java
Here is the source code of the Java Program to Check if two arrays are equal or not.
Code:
import java.util.Scanner;
public class p28 {
static void sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
static int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
return 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,j=0;
size=sc.nextInt();
System.out.println("Enter the size of the 2nd array:");
int size2=sc.nextInt();
int arr[ ]=new int[size];
int arr2[ ]=new int[size2];
System.out.println("Enter the Element of the array:");
for(j=0;j<size;j++)
{
arr[j]=sc.nextInt();
}
System.out.println("Enter the Element of the array:");
for(j=0;j<size2;j++)
{
arr2[j]=sc.nextInt();
}
if(check_equals(arr,arr2,size,size)==0)
System.out.println("Not same....");
else
System.out.println("same....");
sc.close();
}
}
public class p28 {
static void sort_arr(int arr[],int size)
{
int i,j,temp;
//Sort the array
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
}
static int check_equals(int arr[],int arr2[],int size,int size2)
{
sort_arr(arr,size);
sort_arr(arr2,size2);
int i;
if(size!=size2)
return 0;
for(i=0;i<size;i++)
{
if(arr[i]!=arr2[i])
return 0;
}
return 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,j=0;
size=sc.nextInt();
System.out.println("Enter the size of the 2nd array:");
int size2=sc.nextInt();
int arr[ ]=new int[size];
int arr2[ ]=new int[size2];
System.out.println("Enter the Element of the array:");
for(j=0;j<size;j++)
{
arr[j]=sc.nextInt();
}
System.out.println("Enter the Element of the array:");
for(j=0;j<size2;j++)
{
arr2[j]=sc.nextInt();
}
if(check_equals(arr,arr2,size,size)==0)
System.out.println("Not same....");
else
System.out.println("same....");
sc.close();
}
}
Enter the size of the array:
3
Enter the size of the 2nd array:
3
Enter the Element of the array:
6
5
4
Enter the Element of the array:
5
6
4
same....
3
Enter the size of the 2nd array:
3
Enter the Element of the array:
6
5
4
Enter the Element of the array:
5
6
4
same....
Program in Python
Here is the source code of the Python Program to Check if two arrays are equal or not.
Code:
arr=[]
arr2=[]
size = int(input("Enter the size of the 1st array: "))
size2 = int(input("Enter the size of the 2nd array: "))
print("Enter the Element of the 1st array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Enter the Element of the 2nd array:")
for i in range(0,size2):
num2 = int(input())
arr2.append(num2)
arr.sort()
arr2.sort()
flag=1
if size != size2:
flag=0
else:
for i in range(0, size):
if arr[i] != arr2[i]:
flag=0
if flag==0:
print("Not same....")
else:
print("same....")
arr2=[]
size = int(input("Enter the size of the 1st array: "))
size2 = int(input("Enter the size of the 2nd array: "))
print("Enter the Element of the 1st array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Enter the Element of the 2nd array:")
for i in range(0,size2):
num2 = int(input())
arr2.append(num2)
arr.sort()
arr2.sort()
flag=1
if size != size2:
flag=0
else:
for i in range(0, size):
if arr[i] != arr2[i]:
flag=0
if flag==0:
print("Not same....")
else:
print("same....")
Input/Output:
Enter the size of the 1st array: 3
Enter the size of the 2nd array: 5
Enter the Element of the 1st array:
4
5
6
Enter the Element of the 2nd array:
7
6
4
7
6
Not same....
Enter the size of the 2nd array: 5
Enter the Element of the 1st array:
4
5
6
Enter the Element of the 2nd array:
7
6
4
7
6
Not same....
Most Recommend Questions:-
More Questions:-
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box