Generating Gray Codes in C

This is C source code, written in the GCC platform (Ubuntu, 14.04 LTS, Linux).

The code generates Gray Codes. At first a palindrome number is being generated. If we Push n elements in the stack, then it generates a palindrome number of length (2^n) -1. This is the special property. Then it can generate Gray code of maximum n bit. Note that, you have to Push elements sequentially in decreasing order (e.g 4,3,2,1). It generates 121312141213121. It can generate grey code of maximum 4 bit. The code:

/*	This is a Process of generating Palindrome number from some given numbers.		This is a Process of generating Grey code from some given numbers.		It actually takes the help of palindrome generation.*/#include #define max 10#define maxim 300#define maxr 550#define maxc 9int pushA (int[], int*, int*);int cpyAC (int[], int[], int*);int palgen (int[], int[], int[], int*, int*);void display (int[], int*);void displayA (int[], int*);int greycode (int A[maxr][maxc], int*, int*, int[], int*);void grycddisp (int A[maxr][maxc], int*, int*);//----------------------------------------------------int pushA (int a[max], int *top, int *item){    printf("
	Enter element: ");    scanf("%d", &(*item));    a[++(*top)] = (*item);    printf("
	Push is successful...
");    return (*top);}//----------------------------------------------------int cpyAC (int a[max], int c[max], int *top){  int i= -1, j= -1, toc;  while (j < *top)    c[++i] = a[++j];  toc= i;		  return (toc);}//----------------------------------------------------int palgen (int a[max], int b[maxim], int c[max], int *top, int *toc){  int i, j, k, tob, x;  j= -1;  i= (*top);			  k= (*toc);			  while (k > -1)  {    b[++j] = c[k];    (*toc)--;    k= (*toc);    while (b[j] != a[i])    {      i--;    }    while (i <= (*top))    {      c[++k] = a[++i];      (*toc)++;          i+= 0;    }    (*toc)--;    k = (*toc);    k+= 0;     }  tob= j;			  return (tob);  }//---------------------------------------------------int greycode (int A[maxr][maxc], int *row, int *col, int b[maxim], int *tob){printf("

		Row= %d, Col= %d

", *row, *col);					//create co-ordinate  int i=0, j, value, k;  while (i < (*row))  {    j=(*col);    while (j >0 )    {      j--;    }    i++;  }					//greycode generation  i= 0;  j= (*col);  while (j > 0)  {    A[i][j] = 0;      j--;  }  while (i< (*row)-1 )  {    i++;     j=(*col);    while (j>0)    {      A[i][j] = A[i-1][j];      j--;    }    k= (*tob);    value = b[k];    (*tob)--;    j= value;//   printf("
	*tob = %d	b[k]= %d	value= %d		i= %d	j= %d", *tob, b[k], value, i,j);    if (A[i][j] == 0)      A[i][j] = 1;    else      A[i][j] = 0;    i+=0;  }}//------------------------------------------------------------void grycddisp (int A[maxr][maxc], int *row, int *col){  int i= 0, j;  while (i < (*row))  {    j=(*col);    while (j >0 )    {      printf("		%d", A[i][j]);      j--;    }    printf("
");    i++;  }}//------------------------------------------------------------void displayA (int a[max], int *top){  int i;  printf("
	You entered...
");  i= (*top);  while (i >= 0)  {    printf("
	%d", a[i--]);  }}//---------------------------------------------------void display (int b[maxim], int *tob){  int i;  printf("
	The items of Stack are shown...{B}
");  i= (*tob);  printf("

		");  while (i >= 0)  {    printf("%d", b[i--]);  }}//----------------------------------------------------void main (void){  int a[max], b[maxim], c[max], item=0, top =(-1), toc, tob;  int A[maxr][maxc], row =1, col =0, bit =0;  char ch;  system ("clear");  printf ("
	This is a Process of generating Grey Code of n-bit.
	Please insert n numbers in decreasing order.
	e.g 4,3,2,1 for 4-bit grey code.

");  if (top == -1)    printf("
	Initially Stack is empty...
");    printf("
	You have to give data, please start insertion.
	Press 'y' for insert, 'n' for exit.

");  ch= getchar();  while (ch == 'y')   {    top= pushA (a, &top, &item);    printf("
	'y' :insert
	'n' :exit.
");    getchar();        ch= getchar();    getchar();    if( top == (max-1) )    {      printf("
	Stack is full.
	Push operation is not possible...

");      break;    }  }  system("clear");  displayA (a, &top);  if(top == -1)    printf("
	Stack is empty...
	Copy is done...

");  else   toc= cpyAC (a, c, &top);			  tob= palgen (a, b, c, &top, &toc);		  if(tob == -1)    printf("
	Stack is empty...

");  else    display (b, &tob);//-------------------------------------------------------  printf("
	Enter the bit number of Grey code: ");  scanf("%d", &bit);  col= bit;  while (bit > 0)  {    row= (row*2);    bit --;  }    greycode (A, &row, &col, b, &tob);  grycddisp (A, &row, &col);  printf ("

	T  H  A  N  K    Y  O  U

");  }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet