Create a function to compare the elements of 2 arrays. Function should receive two pointers to an array
and then check if the elements are same in both arrays or not. If both arrays are same return true,
otherwise false.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <malloc.h>
#define bool int
#define false 0
#define true 1
//define a function wich compare two arrays
bool isEqual(int *a,int *b,int size)
{
for(int i=0;i<size;i++)
if(a[i]!=b[i])
return false;
return true;
}
int main(void) {
int n;
printf("Size of arrays:");
scanf("%i",&n);
int *a=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
scanf("%i",&a[i]);
}
int *b=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
scanf("%i",&b[i]);
}
int ans=isEqual(a,b,n);
printf("%s\n",(ans)?"true":"false");
free(a);
free(b);
return 0;
}
Comments
Leave a comment