- How to check if two arrays are equal in C
- How to compare arrays element by element
- How to use memcmp for array comparison
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to check if two arrays are equal.
Two arrays are considered equal if they have the same length and all corresponding elements are equal. This is used in many real-world applications, such as:
- Comparing two data sets for equality
- Checking if two strings (character arrays) are equal
- Validating data integrity in file transfers
- Comparing configuration arrays in software
š” Key Point: In C, we cannot compare arrays directly using the == operator. We must compare each element individually using a loop or use the memcmp() function from the string.h library.
C Program to Check if Two Arrays are Equal
#include <stdio.h>
int main() {
int n1, n2, i;
int equal = 1; // Flag: 1 = equal, 0 = not equal
// ====== FIRST ARRAY ======
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
// ====== SECOND ARRAY ======
printf("\nEnter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// ====== CHECK EQUALITY ======
// First check if arrays have same size
if(n1 != n2) {
equal = 0;
} else {
// Compare each element
for(i = 0; i < n1; i++) {
if(arr1[i] != arr2[i]) {
equal = 0;
break; // Exit loop early if mismatch found
}
}
}
// ====== DISPLAY ======
printf("\nFirst array: ");
for(i = 0; i < n1; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for(i = 0; i < n2; i++) {
printf("%d ", arr2[i]);
}
if(equal == 1) {
printf("\n\nā
Both arrays are EQUAL.\n");
} else {
printf("\n\nā Arrays are NOT equal.\n");
}
return 0;
}
Sample Output
Enter the number of elements in first array: 5 Enter 5 elements: 10 20 30 40 50 Enter the number of elements in second array: 5 Enter 5 elements: 10 20 30 40 50 First array: 10 20 30 40 50 Second array: 10 20 30 40 50 ā Both arrays are EQUAL.
When Arrays are Not Equal:
Enter the number of elements in first array: 5 Enter 5 elements: 10 20 30 40 50 Enter the number of elements in second array: 5 Enter 5 elements: 10 20 30 40 60 First array: 10 20 30 40 50 Second array: 10 20 30 40 60 ā Arrays are NOT equal.
When Arrays have Different Sizes:
Enter the number of elements in first array: 4 Enter 4 elements: 10 20 30 40 Enter the number of elements in second array: 5 Enter 5 elements: 10 20 30 40 50 First array: 10 20 30 40 Second array: 10 20 30 40 50 ā Arrays are NOT equal.
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n1, n2;ā sizes of the two arraysint i;ā loop counterint equal = 1;ā flag variable (1 = equal, 0 = not equal)
- Read Arrays: Reads the size and elements of both arrays.
- Check Equality:
- Step 1: Check if sizes are equal. If not, arrays are not equal.
- Step 2: If sizes are equal, loop through each element and compare
arr1[i]witharr2[i]. - Step 3: If any mismatch is found, set
equal = 0andbreakout of the loop.
- Display Results: Prints both arrays and a message indicating whether they are equal.
- Return:
return 0;indicates successful program execution.
š Note: The break statement is used to exit the loop early when a mismatch is found. This makes the program more efficient for large arrays where mismatches occur early.
Algorithm to Check Array Equality
Step-by-step algorithm:
- Start
- Read the elements of first array (arr1) with size n1
- Read the elements of second array (arr2) with size n2
- If
n1 != n2:- Print "Arrays are not equal"
- Exit
- For
i = 0ton1-1:- If
arr1[i] != arr2[i]:- Print "Arrays are not equal"
- Exit
- If
- Print "Arrays are equal"
- End
Using memcmp Function (More Efficient)
The memcmp() function from string.h library provides a faster way to compare arrays. It compares a block of memory byte by byte.
#include <stdio.h>
#include <string.h> // Required for memcmp
int main() {
int n1, n2, i;
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("\nEnter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Display arrays
printf("\nFirst array: ");
for(i = 0; i < n1; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for(i = 0; i < n2; i++) {
printf("%d ", arr2[i]);
}
// Check equality using memcmp
if(n1 == n2 && memcmp(arr1, arr2, n1 * sizeof(int)) == 0) {
printf("\n\nā
Both arrays are EQUAL.\n");
} else {
printf("\n\nā Arrays are NOT equal.\n");
}
return 0;
}
Sample Output:
Enter the number of elements in first array: 5 Enter 5 elements: 10 20 30 40 50 Enter the number of elements in second array: 5 Enter 5 elements: 10 20 30 40 50 First array: 10 20 30 40 50 Second array: 10 20 30 40 50 ā Both arrays are EQUAL.
How memcmp Works
memcmp(arr1, arr2, n1 * sizeof(int))compares the memory blocks- Parameters:
arr1ā pointer to first arrayarr2ā pointer to second arrayn1 * sizeof(int)ā number of bytes to compare
- Return Value:
0ā arrays are equal< 0ā first difference is smaller in arr1> 0ā first difference is larger in arr1
- Advantage: Faster than manual loops for large arrays
Time and Space Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Element-by-Element (Loop) | O(n) | O(1) |
| memcmp() | O(n) | O(1) |
š» Practice Exercise
Challenge 1: Modify the program to check if two arrays are equal ignoring order (i.e., same elements but possibly in different positions).
Challenge 2: Compare two arrays of strings (character arrays) for equality.
š Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int n1, n2, i, j;
int equal = 1;
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements:\n", n1);
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("\nEnter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements:\n", n2);
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Check if sizes are equal
if(n1 != n2) {
equal = 0;
} else {
// For each element in arr1, check if it exists in arr2
int visited[n2];
for(i = 0; i < n2; i++) {
visited[i] = 0;
}
for(i = 0; i < n1; i++) {
int found = 0;
for(j = 0; j < n2; j++) {
if(arr1[i] == arr2[j] && visited[j] == 0) {
found = 1;
visited[j] = 1;
break;
}
}
if(found == 0) {
equal = 0;
break;
}
}
}
if(equal == 1) {
printf("\nā
Both arrays have the same elements (ignoring order).\n");
} else {
printf("\nā Arrays do NOT have the same elements.\n");
}
return 0;
}
Frequently Asked Questions
1. How do you check if two arrays are equal in C?
First, check if both arrays have the same length. Then, compare each corresponding element using a loop. If all elements match, the arrays are equal.
2. Can I use the == operator to compare two arrays in C?
No, the == operator compares the memory addresses of the arrays, not their contents. You must compare each element individually or use memcmp().
3. What is the difference between memcmp and element-by-element comparison?
memcmp() is a library function that compares memory blocks byte by byte. It's often faster and more concise. The element-by-element loop gives you more control and allows you to add custom comparison logic.
4. How do I compare two arrays of different data types?
Arrays of different data types cannot be compared directly because they store different amounts of memory. You would need to convert or cast the data before comparison.
5. How do I check if two arrays are equal ignoring order?
Sort both arrays and then compare them, or use a frequency-based approach (count occurrences of each element). The practice exercise above provides a solution for this.
š” Tip: For comparing large arrays, memcmp() is generally faster. For arrays with custom comparison logic, use the element-by-element loop.