In C++ t%20arrays. pdf 211 Equivalent Arrays Consider the two arrays a and b. a:
ID: 3742369 • Letter: I
Question
In C++ t%20arrays. pdf 211 Equivalent Arrays Consider the two arrays a and b. a: b: It is possible to transform array a into array b by right shifting each element of a to the "right" three places. If an element "falls off the back of the array have it come around the front and keep counting positions. That is how 3 in array ended up in the first position of array b. One way to look at this is to imagine that we are moving the element around in a circular manner. In the example above, we have right shifted the array 3 positions to the right. Definition: Let a and b be two integer arrays of the same length. We say that they are "shift equivalent" if array a can be right shifted to create array b. Problem Write a function bool equivalent(int all, int bll, int n) which takes two arrays a and b of length n and returns true is they are shift equivalent and false otherwise.Explanation / Answer
Here's the working code in C++,
#include <iostream>
using namespace std;
bool equivalent(int a[], int b[], int n)
{
int size = 0;
while(size < n) {
int temp = a[n - 1], i;
//Right Shifting the array elements by one position
for (i = n - 1; i > 0; i--) {
a[i] = a[i - 1];
}
a[0] = temp;
//After Shifting Checking whether they are equivalent for not;
int j = 0;
bool flag = false;
for(j = 0; j < n; j++) {
//If not then continue with the outermost loop until it reached n;
if(a[j] != b[j]) {
flag = true;
break;
}
}
//If they are equivalent return true;
if(flag == false) {
return true;
}
size++;
}
return false;
}
int main() {
// your code goes here
int a[] = {1, 2, 3, 4, 5};
int b[] = {5,4,3,2,1};
int n = sizeof(a) /
sizeof(a[0]);
bool value = equivalent(a, b, n);
cout << value;
return 0;
}