-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrayCode.cpp
More file actions
215 lines (194 loc) · 5.6 KB
/
GrayCode.cpp
File metadata and controls
215 lines (194 loc) · 5.6 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//============================================================================
// Name : GrayCode.cpp
// Author : Imogen Cleaver-Stigum & Jyalu Wu
// Version : 11/13/19
// Copyright : 2019 IMOLU
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#include "GrayCode.h"
using namespace std;
int main() {
int n = 4;
//initializing grayCodes
char **grayCodes;
grayCodes = new char *[(int) pow(2,n)];
for(int i = 0; i < (int) pow(2,n); i++)
grayCodes[i] = new char[n];
newGrayCode(n, grayCodes);
printTable(n, grayCodes);
return 0;
}
/*
* Generates the Binary Reflected Gray Code of order n recursively.
* @param n The given order
* @param grayCodes The given empty array to store the Gray Codes
*/
void newGrayCode(int n, char **grayCodes) {
int numGrayCodes = (int) pow(2, n);
// char grayCodesUpper[numGrayCodes][n];
char **grayCodesUpper;
grayCodesUpper = new char *[numGrayCodes / 2];
for(int i = 0; i < numGrayCodes / 2; i++)
grayCodesUpper[i] = new char[n];
// char grayCodesLower[numGrayCodes][n];
char **grayCodesLower;
grayCodesLower = new char *[numGrayCodes / 2];
for(int i = 0; i < numGrayCodes / 2; i++)
grayCodesLower[i] = new char[n];
if (n == 1) {
// make list L containing bit strings 0 and 1 in this order
grayCodes[0][0] = '0';
grayCodes[1][0] = '1';
}
else {
newGrayCode(n - 1, grayCodesUpper);
// copy list L1 to list L2 in reversed order
for (int i = 0; i < numGrayCodes/2; i++) {
for (int j = 0; j < n-1; j++) {
grayCodesLower[i][j] = grayCodesUpper[numGrayCodes/2-i-1][j];
}
}
// add 0 in front of each bit string in list L1
for (int i = 0; i < numGrayCodes/2; i++) { // copy L1 to L
grayCodesUpper[i][n-1] = '0';
}
for (int i = 0; i < numGrayCodes/2; i++) { // copy L1 to L
grayCodesLower[i][n-1] = '1';
}
// copy L1 and L2 into L
for (int i = 0; i < numGrayCodes/2; i++) {
for (int j = 0; j < n; j++) {
grayCodes[i][j] = grayCodesUpper[i][j];
}
}
for (int i = 0; i < numGrayCodes/2; i++) {
for (int j = 0; j < n; j++) {
grayCodes[i+numGrayCodes/2][j] = grayCodesLower[i][j];
}
}
}
}
/*
* Prints the given Gray Codes in table format, specifying the index, the actual
* Gray Code, the children who will be in the photo, and which action was taken.
* @param n The given order
* @param grayCodes The given empty array to store the Gray Codes
*/
void printTable(int n, char** grayCodes) {
const char separator = ' ';
const int indexWidth = 10;
const int grayCodeWidth = 15;
const int childrenWidth = 30;
const int actionWidth = 11;
const int size = (int) pow(2,n);
char* currentGrayCode = new char[n];
int* abacadabra = new int[size];
string childrenList = "";
string action = "";
abacaba(n, abacadabra);
// print out headers
cout << left << setw(indexWidth) << setfill(separator) << "Index";
cout << left << setw(grayCodeWidth) << setfill(separator) << "Gray Code";
cout << left << setw(childrenWidth) << setfill(separator) << "Child(ren) in Photo";
cout << left << setw(actionWidth) << setfill(separator) << "Action";
cout << endl;
cout << left << setw(indexWidth + grayCodeWidth + childrenWidth + actionWidth) << setfill('-') <<"";
cout << endl;
// for all of the gray codes
for (int i = 0; i < size; i++) {
// get the next gray code
for (int j = n-1; j >= 0; j--) {
currentGrayCode[n - j - 1] = grayCodes[i][j];
}
// get the list of children
for (int j = 0; j < n; j++) {
if (currentGrayCode[j] == '1') {
switch (j) {
case 3 :
childrenList = childrenList + "Alice ";
break;
case 2 :
childrenList = childrenList + "Bowie ";
break;
case 1 :
childrenList = childrenList + "Chris ";
break;
case 0 :
childrenList = childrenList + "Dylan ";
break;
}
}
}
// get the action
if (i != 0) {
switch (abacadabra[i - 1]) {
case 0 :
action = action + "Alice ";
if (currentGrayCode[3] == '1') {
action = action + "Come Here";
}
else {
action = action + "Go Away";
}
break;
case 1 :
action = action + "Bowie ";
if (currentGrayCode[2] == '1') {
action = action + "Come Here";
}
else {
action = action + "Go Away";
}
break;
case 2 :
action = action + "Chris ";
if (currentGrayCode[1] == '1') {
action = action + "Come Here";
}
else {
action = action + "Go Away";
}
break;
case 3 :
action = action + "Dylan ";
if (currentGrayCode[0] == '1') {
action = action + "Come Here";
}
else {
action = action + "Go Away";
}
break;
}
}
cout << left << setw(indexWidth) << setfill(separator) << i;
cout << left << setw(grayCodeWidth) << setfill(separator) << currentGrayCode;
cout << left << setw(childrenWidth) << setfill(separator) << childrenList;
cout << left << setw(actionWidth) << setfill(separator) << action;
cout << endl;
childrenList = "";
action = "";
}
}
/*
* Generates an abacaba sequence, represented by n different integers. For example,
* if the function is given n=3, the sequence would be 0102010 and if it was given
* n=4, the sequence would be 010201030102010
* @param n, The given number of integers
* @param abracadabra, The given empty array to store the sequence
*/
void abacaba(int n, int* abracadabra) {
if (n == 1) {
abracadabra[n-1] = 0;
}
else {
abacaba(n-1, abracadabra);
abracadabra[(int) (pow(2,n-1))-1] = n-1;
for (int i = 0; i < (int) pow(2,n-1)-1; i++) {
abracadabra[i + (int) pow(2,n-1)] = abracadabra[i];
}
}
}