-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrayCode.cpp
132 lines (103 loc) · 2.38 KB
/
GrayCode.cpp
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
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<string> gray;
int arr[32]={0};
int i=1;
//Taking Input
void input()
{
cin>>n;
}
//Finding Binary form
void find_binary(int n)
{
while(n>2)
{
arr[i]=n%2;
if(n%2==0)
n=n/2;
else
n=(n+1)/2;
i++;
}
/* for (int j = i ; j >0; j--)
cout << arr[j]; */
}
// Print Array
void print()
{
if(n%2==0)
{
cout << "Gray Code of Length "<<n<<" is :"<<endl;
for (int p = 0 ; p < gray.size() ; p++ )
cout<<gray[p] << endl;
}
else
{
cout << "Open Gray Code of Length "<<n<<" is :"<<endl;
if(n==1)
{
cout<<0;
}
for (int p = 0 ; p < gray.size() ; p++ )
cout<<gray[p] << endl;
}
}
//Code for Open Gray Code
int open_gray_code()
{
int size = gray.size();
//cout<<size<<endl;
for (int k = size-1 ; k >= 0 ; k--)
gray.push_back(gray[k]);
// append 0 to the first half
for (int k = 0 ; k < size ; k++)
gray[k] = "0" + gray[k];
// append 1 to the second half
for (int k = size ; k < 2*size ; k++)
gray[k] = "1" + gray[k];
gray.pop_back();
}
//Code for gray Code
int gray_code()
{
int size = gray.size();
//cout<<size<<endl;
for (int k = size-1 ; k >= 0 ; k--)
gray.push_back(gray[k]);
// append 0 to the first half
for (int k = 0 ; k < size ; k++)
gray[k] = "0" + gray[k];
// append 1 to the second half
for (int k = size ; k < 2*size ; k++)
gray[k] = "1" + gray[k];
}
//checking for gray code and open gray code
void find_gray_code()
{
// base case
if (n <= 1)
return;
// start with 2 legth pattern
gray.push_back("0");
gray.push_back("1");
for(int j=i-1;j>0;j--)
{
if(arr[j]==0)
gray_code();
else
open_gray_code();
}
}
int main() {
input();
find_binary(n);
find_gray_code();
if(n<1)
cout<<"Not exist"<<endl;
else
print();
return 0;
}