Compare
Compare Two Arrays Same or Not
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int compare_array(int *a, int size_a, int *b, int size_b);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, size_a = 5;
int b[TOTAL_SIZE] = {10, 20, 30, 40, 60}, size_b = 5;
int c[TOTAL_SIZE] = {10, 20, 30, 40}, size_c = 4;
print_array(a, size_a);
print_array(b, size_b);
if(compare_array(a, size_a, a, size_a)){
printf("Both Array are Same\n");
}else{
printf("Different Arrays\n");
}
if(compare_array(a, size_a, b, size_b)){
printf("Both Array are Same\n");
}else{
printf("Different Arrays\n");
}
if(compare_array(a, size_a, c, size_c)){
printf("Both Array are Same\n");
}else{
printf("Different Arrays\n");
}
return 0;
}
int compare_array(int *a, int size_a, int *b, int size_b){
if(size_a == size_b){
for(int i=0; i<size_a; i++){
if(a[i] != b[i]){
return 0;
}
}
return 1;
}else{
return 0;
}
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Last updated