1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
using namespace std;
/** Prints all elements of an array with comma separators
* @param int a[] (array to be printed)
* @param int size (size of array to be printed)
*/
void print_array(const int a[], const int size)
{
for(int i = 0; i < size; i++)
{
if(i > 0)
{
cout << ", ";
}
cout << a[i];
}
}
/** Shifts all elements one position upwards and
* moves final element to 0 index
* @param int a[] (array to modify)
* @param int size (size of array)
*/
void shift(int a[], int size)
{
int i;
int j = size - 1;
int last = a[j];
for(i = 0; i < size; i++)
{
a[j] = a[j-1];
j--;
}
a[0] = last;
}
/**Determines if the elements of two arrays are in the exact same order
* @param int a[] (first array)
* @param int b[] (second array)
* @param int size (size of array)
*/
bool exact_order(int a[], int b[], int size)
{
bool result = true;
for(int i = 0; i < size; i++)
{
if(a[i] != b[i])
{
result = false;
}
}
return result;
}
/**Determines if elements in 2 arrays are both in the same circular order
* @param int a[] (first array)
* @param int b[] (second array)
* @return result (true/false)
*/
bool circular(int a[], int b[], int size)
{
bool result = false;
if(exact_order(a, b, size))
{
result = true;
}
else
{
for(int i = 0; i < size; i++)
{
shift(b, size);
if(exact_order(a, b, size))
{
result = true;
}
}
}
return result;
}
int main()
{
const int SIZE = 4;
int a[] = {1, 2, 3, 4};
int b[] = {2, 3, 4, 1};
if(circular(a, b, SIZE))
{
cout << "Circular Order!";
}
else
{
cout << "Not Circular Order!";
}
}
|