Hello I need to write a function decribed below by using C: int same_contents(in
ID: 3564140 • Letter: H
Question
Hello I need to write a function decribed below by using C:
int same_contents(int a[], int b[], int n);
* returns 1 if a[0..n-1] and b[0..n-1] have the same contents;
* returns 0 otherwiwse.
* 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.
Explanation / Answer
void main()
{
int check(int[],int[])
int a[100],b[100];
cin >>n //Enter limit
same_contents(a,b,n);
}
int check(int a[],int b[],n)
{
int i,j,flag=1;
//Sort First Array
int i, j, temp;
for (i = 0; i < n- 1); ++i)
{
for (j = 0; j < n- 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
//Sort Second Array
for (i = 0; i < n- 1); ++i)
{
for (j = 0; j < n- 1 - i; ++j )
{
if (b[j] > b[j+1])
{
temp = b[j+1];
b[j+1] = b[j];
b[j] = temp;
}
}
}
for (i=0;i<n;i++)
{
if(a[i]!=b[i])
{
flag=0;
return 0;
}
else if(flag=1)
{
return 1;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.