Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * * Function: same_contents * * Parameters: * integer array a[]: first array

ID: 640838 • Letter: #

Question

/**

*

* Function: same_contents

*

* Parameters:

* integer array a[]: first array -- NOT to be modified

* integer array b[]: second array -- NOT to be modified

* integer n: Length of both arrays a[] & b[]

*

* Description:

* returns 1 if a[0..n-1] and b[0..n-1] have the same contents;

* returns 0 otherwise.

*

* a[] and b[] "have the same contents" when:

*

* - every integer in a[] is also in b[]

* - every integer in b[] is also in a[]

* - all such common values appear exactly the same

* number of times in both a[] and b[]

*

* examples:

*

* a = {1, 2, 3}, b = {2, 3, 4} NO

* a = {1, 2, 3}; b = {2, 3, 1} YES

* a = {1, 2, 2}; b = {2, 2, 1} YES

* a = {1, 2, 2}; b = {2, 1, 1} NO

* a = {1, 1, 2, 2, 2}; b = {2, 1, 2, 1, 2} YES

*

* RULES: You may NOT re-order the elements of the arrays.

*/

int same_contents(int a[], int b[], int n);

PLEASE program a Function in C and use stdio.h library

Explanation / Answer

#include <stdio.h>

int same_contents(int a[], int b[], int n){
   int c[10] = {0};
   int i = 0;
   for(i = 0; i < n; ++i){
       c[a[i]]++;
   }
   for(i = 0; i < n; ++i){
       c[b[i]]--;
   }
   for(i = 0; i < 10; ++i){
       if(c[i] != 0){
           return 0;
       }
   }
   return 1;
}

int main(){
   int a[] = {1, 2, 2};
   int b[] = {2, 1, 1};
   if(same_contents(a, b, 3)){
       printf("YES ");
   }
   else{
       printf("NO ");
   }
   return 0;
}