I am having issues writing the loop language that i am using is C .docx
1. I am having issues writing the loop language that i am using is
C Function: same_contents * Description: determines if
arrays a[] and b[] contain * exactly the same values,
but not necessarily * in the same order. *
returns 1 if they do have the same contents * returns 0
otherwise. * Note: if there are multiple occurrences of
the * same value, both arrays must contain the same *
number of such occurrences to be considered * to have
the same contents. * * Assumptions: parameter n
specifies the length of both a[] and * b[] * *
Restrictions: the arrays a[] and b[] may not be modified. */ int
same_contents(int a[], int b[], int n)
Solution
compare.c
#include <stdio.h>
int same_contents(int a[], int b[], int n);
int main()
{
int a[] = {1 , 2,4 ,2 ,5, 3,3};
int b[] = {1 , 4,5 ,3, 2, 2, 3};
int n = 7;
int value;
2. value = same_contents(a, b, n);
printf(" The return value is %d ", value);
return 0;
}
int same_contents(int a[], int b[], int n){
int i, j;
int count = 0, temp;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Array a elements after sorting: ");
for(i=0; i<n; i++){
printf("%d ",a[i]);
}
printf(" ");
3. printf(" ");
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (b[i] > b[j])
{
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
printf("Array b elements after sorting: ");
for(i=0; i<n; i++){
printf("%d ",b[i]);
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
if(a[i] != b[i]){
count = 1;
}
}
}
4. if(count == 0){
return 1;
}
else
return 0;
}
Output:
Array a elements after sorting:
1 2 2 3 3 4 5
Array b elements after sorting:
1 2 2 3 3 4 5
The return value is 1