Question #333178

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.


Expert's answer



#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS