Answer to Question #333178 in C for lota

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.


1
Expert's answer
2022-04-24T17:16:02-0400



#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS