65 660
Assignments Done
99,2%
Successfully Done
In October 2018

Answer to Question #76247 in C++ for Erik Rubino

Question #76247
I am learning coding and my teacher is not good so I need help on an assignmentsWrite a function

bool same_elements(int a[], int b[], int size)
that checks whether two arrays have the same elements in some order, with the same multiplicities. For example,

1 4 9 16 9 7 4 9 11

and

11 1 4 9 16 9 7 4 9

would be considered identical, but

1 4 9 16 9 7 4 9 11

and

11 11 7 9 16 4 1 4 9

would not. You will probably need one or more helper functions.
Expert's answer
#include <iostream>

using namespace std;

bool same_elements(int a[], int b[], int size) {
bool ident = false;
for (int i = 0; i < size; i++) {
ident = true;
for (int j = 0; j < size; j++) {
if (a[j] != b[(j + i) % size]) ident = false;
}
if (ident) break;
}
return ident;
}

int main()
{
int a[] = { 1, 4, 9, 16, 9, 7, 4, 9, 11 };
int b[] = { 11, 1, 4, 9, 16, 9, 7, 4, 9 };
int c[] = { 11, 11, 7, 9, 16, 4, 1, 4, 9 };

if (same_elements(a, b, 9)) {
cout << "Arrays A and B are identical" << endl;
} else {
cout << "Arrays A and B are not identical" << endl;
}

if (same_elements(a, c, 9)) {
cout << "Arrays A and C are identical" << endl;
} else {
cout << "Arrays A and C are not identical" << endl;
}

// waiting for press ane key
cout << endl;
cout << "Press any key...";
cin.get();

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

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions