ADVERTISEMENT
ADVERTISEMENT

Home » Code snippets » C/C++ Code Snippets

C program to compare two arrays.

In this program we will learn how to compare two one dimensional arrays in c programming language?

In this program we will read two one dimensional arrays of 5 elements and compare them.

Here are the functions which we designed to read, print and compare arrays

  • readArray()
  • printArray()
  • compareArray()

readArray() will read array of 5 elements.

printArray() will print array of 5 elements.

compareArray() will compare elements of both of the array elements and returns 0 if all elements are equal otherwise function will return 1.

C program (Code Snippet) - Compare Two Arrays

Let’s consider the following example:

/*c program to compare two arrays*/

#include <stdio.h>

//function to read array elements
void readArray(int arr[], int size) 
{ 
    int i =0; 
 
    printf("\nEnter elements : \n"); 
 
    for(i=0; i < size; i++) 
    { 
        printf("Enter arr[%d] : ",i); 
        scanf("%d",&arr[i]); 
    } 
} 

//print array elements
void printArray(int arr[],int size) 
{ 
    int i =0; 
 
    printf("\nElements are : "); 
 
    for(i=0; i < size; i++) 
    { 
        printf("\n\tarr[%d] : %d",i,arr[i]); 
    } 
    printf("\n"); 
} 

//function to compare array elements
char compareArray(int a[],int b[],int size)	{
	int i;
	for(i=0;i<size;i++){
		if(a[i]!=b[i])
			return 1;
	}
	return 0;
}

int main(){
	int a[5],b[5];
	readArray(a,5);
	readArray(b,5);
	
	if(compareArray(a,b,5)==0){
		printf("Arrays have same elements.\n");
		printArray(a,5);
	}
	else{
		printf("Arrays have different elements.\n");
	}
		
	return 0;
}
ADVERTISEMENT

Output

    First Run:
    Enter elements :
    Enter arr[0] : 10 
    Enter arr[1] : 20 
    Enter arr[2] : 30 
    Enter arr[3] : 40 
    Enter arr[4] : 50 

    Enter elements :
    Enter arr[0] : 10 
    Enter arr[1] : 20 
    Enter arr[2] : 30 
    Enter arr[3] : 40 
    Enter arr[4] : 50 
    Arrays have same elements.

    Elements are :
	    arr[0] : 10 
	    arr[1] : 20 
	    arr[2] : 30 
	    arr[3] : 40 
	    arr[4] : 50 

    Second Run:	
    Enter elements :
    Enter arr[0] : 10 
    Enter arr[1] : 20 
    Enter arr[2] : 30 
    Enter arr[3] : 40 
    Enter arr[4] : 50 

    Enter elements :
    Enter arr[0] : 10 
    Enter arr[1] : 20 
    Enter arr[2] : 40 
    Enter arr[3] : 40 
    Enter arr[4] : 40
    Arrays have different  elements.

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.