-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateGrayCode.c
More file actions
executable file
·45 lines (39 loc) · 868 Bytes
/
CreateGrayCode.c
File metadata and controls
executable file
·45 lines (39 loc) · 868 Bytes
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
#include <stdio.h>
#include <math.h>
#define K 3
int G[(int)pow(2,K)][K];
void mgc(int b);
int main(void) {
int i,j,a=1;
for(i=0;i<=pow(2,K)-1;i++){
for(j=0;j<=K-1;j++)
G[i][j]=0;
}
G[1][K-1]=1;
mgc(a);
return 0;
}
void mgc(int b){
int i,j,s,g;
if (b==K){
for (i=0;i<=(int)pow(2,K)-1;i++){
printf("The number %d in Gray code is ",i);
for(j=0;j<=K-1;j++)
printf("%d",G[i][j]);
printf("\n");
scanf("%d",&s);
}
}
else{
for(i=b;i>0;i--){
g=1;
for(j=((int)pow(2,b)-1);j>=0;j--){
G[(j+g)][K-i]= G[j][K-i];
g+=2;
}
}
for(i=pow(2,b);i<=(int)pow(2,b+1);i++)
G[i][K-(b+1)]=1;
mgc(b+1);
}
}