C program to find whether the given two arrays are the same or not. For this, our necessary inputs are the number of elements in the two arrays and the data values of the two arrays. Our expected output for the given problem statement is whether the two arrays are same or not.
Check if Two Arrays Are Equal in C Using Function
To read our given set of inputs at runtime in C, we can use a well-known predefined function, scanf(). This function reads input of any primitive datatype (int, float, char, etc) at runtime. The format specifier mentioned decides the type of input to be taken.
Since all the inputs for the given problem are integers, we’ll use ‘%d’ as our format specifier. We first read the size of two arrays (n and m) and create two arrays of the given size. We then iterate through each array from 0th index to the last and read each data value of the two input arrays.
printf(“\nEnter number of elements in 1st array : “);
scanf(“%d”,&n);
printf(“\nEnter number of elements in 2nd array : “);
scanf(“%d”,&m);
int a1[n];
int a2[m];
printf(“\nEnter %d elements of an 1st array :\n”,n);
for(i = 0; i < n; i++) {
scanf(“%d”,&a1[i]);
}
printf(“\nEnter %d elements of an 2nd array :\n”,m);
for(i = 0; i < m; i++) {
scanf(“%d”,&a2[i]);
}
After getting all the necessary inputs, we make a function call to the user-defined function (checkArray). This function contains the logic necessary to find whether the two arrays are the same or not. It takes all the inputs as parameter and returns an integer number to the main method.
int c;
c=checkArray(n,m,a1,a2);
In this function, we first check whether the size of the two arrays are the same i.e., if m=n. If yes then, we iterate from from first index (0) to last index (n) of first array. For each data value of the first array, we iterate from first index (0) to the last index (m) of the second array and check if any data value of the second array is same as the data value of the ith index of the first array.
If it is same then, we make the that data value in second array (c[j]) null indicating that, we have already matched this element with one of the elements of the first array. We also increment the count variable (c) which was initially zero by 1 and break the iteration of inner loop (second array).
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
if(array1[i]==array2[j]) {
array2[j]=’\n’;
c++; break;
}
}
}
After the end of all the iterations, we check whether the value of count variable (c) equals to the size of array (n). If yes then it means that, the two arrays are the same and it returns 1 to the main method.
All this process is within the condition that both the array sizes are equal. So, for any other case other than mentioned above i.e., if the count variable (c) is not equal to size or whether the size of two arrays are not equal then, it returns 0 to the main method.
In the main method, if the value returned is 1 then, it displays that, the two arrays are the same using printf() function on the output console screen. Else, it displays that, the two arrays are not the same using the printf() function on the output console screen.
if(c==1)
printf(“Same”);
else
printf(“Not same”);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<stdio.h> #include<stdlib.h> int checkArray(int n,int m,int *array1,int *array2) { int i,j,c=0; if(n==m) { for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(array1[i]==array2[j]) { array2[j]='\n'; c++; break; } } } if(c==n) return 1; } return 0; } int main() { int n, m,i,j,c=0; printf("\nEnter number of elements in 1st array : "); scanf("%d",&n); printf("\nEnter number of elements in 2nd array : "); scanf("%d",&m); int a1[n]; int a2[m]; printf("\nEnter %d elements of an 1st array :\n",n); for(i = 0; i < n; i++) { scanf("%d",&a1[i]); } printf("\nEnter %d elements of an 2nd array :\n",m); for(i = 0; i < m; i++) { scanf("%d",&a2[i]); } c=checkArray(n,m,a1,a2); if(c==1) printf("Same"); else printf("Not same"); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Enter number of elements in 1st array : 4 Enter number of elements in 2nd array : 4 Enter 4 elements of an 1st array : 1 0 1 2 Enter 4 elements of an 2nd array : 1 2 0 3 Not same |
Check if Two Arrays Are Equal C Programming – Type – 2
In the above method, we have seen the use of functions and the logic to determine whether the two arrays are same or not. The main advantages on using functions is that, we can make the code reusable and enhance the readability of the code.
For example, if the same logic is required somewhere else in the code in the future then, with functions we just have to make a function call and pass parameters accordingly instead of rewriting the same set of statements or logic all over again.
Also, if some part of the code is to be found then, with functions we can directly go to the corresponding function containing that logic instead of going all over main method. This makes the code lot cleaner too.
But that doesn’t mean that, we can’t write the entire code in main method. If we are sure we don’t need the logic elsewhere then, there is no harm in writing everything within main method. Here also the same logic is used and we read inputs using scanf() at runtime.
Then, we check if the two arrays are same as above. We display the same accordingly using printf(). The code is written below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include<stdio.h> #include<stdlib.h> int main() { int n, m,i,j,c=0; printf("\nEnter number of elements in 1st array : "); scanf("%d",&n); printf("\nEnter number of elements in 2nd array : "); scanf("%d",&m); if(n==m) { int array1[n]; int array2[m]; printf("\nEnter %d elements of an 1st array :\n",n); for(i = 0; i < n; i++) { scanf("%d",&array1[i]); } printf("\nEnter %d elements of an 2nd array :\n",m); for(i = 0; i < m; i++) { scanf("%d",&array2[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(array1[i]==array2[j]) { array2[j]='\n'; c++; break; } } } if(c==n) printf("Same"); else printf("Not same"); } else printf("Not same"); return 0; } |