dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Dec 11, 2015

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Generation of Equivalent Binary Code of Decimal Digits

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

Here is the process for generating the Equivalent Binary Code of a decimal digit. First, a palindrome number is being generated. If we Push n elements in the stack, it generates a palindrome number of length (2^n) -1.

This is the special property. Then it can generate Binary code of maximum n bit. Note that, we have to Push elements sequentially in Decreasing order. e.g 4,3,2,1. It generates 121312141213121, so now it can generate Binary code of maximum 4 bit.

/*
   This is a Process of generating Palindrome number from some given numbers.   
   This is a Process of generating binary 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 9

int 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 binarycode (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("\n\tEnter element: ");
    scanf("%d", &(*item));
    a[++(*top)] = (*item);
    printf("\n\tPush is successful...\n");
    return (*top);

}

//----------------------------------------------------

int cpyAC (int a[max], int c[max], int *top)
{

  int i= -1, j= -1, toc;
  while (j      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      {

      c[++k] = a[++i];
      (*toc)++;
    
      i+= 0;
    }

    (*toc)--;
    k = (*toc);
    k+= 0;
   
  }
  tob= j;         
  return (tob);
  
}

//---------------------------------------------------

int binarycode (int A[maxr][maxc], int *row, int *col, int b[maxim], int *tob)
{

printf("\n\n\t\tRow= %d, Col= %d\n\n", *row, *col);               //create co-ordinate
  int i=0, j, value, k;

  while (i    {
    j=(*col);
    while (j 0 )
    {
      j--;
    }
    i++;
  }

               //binarycode generation
  i= 0;
  j= (*col);
  while (j  0)
  {
    A[i][j] = 0;  
    j--;
  }

  while (i   {
    i++; 
    j=(*col);

    while (j0)
    {
      A[i][j] = A[i-1][j];
      j--;
    }


    k= (*tob);
    value = b[k];
    (*tob)--;

    j= value;

    for(; j0; 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    {
    j=(*col);
    while (j 0 )
    {
      printf("\t\t%d", A[i][j]);
      j--;
    }
    printf("\n");
    i++;
  }

}

//------------------------------------------------------------

void displayA (int a[max], int *top)
{

  int i;
  printf("\n\tYou entered...\n");
  i= (*top);
  while (i = 0)
  {
    printf("\n\t%d", a[i--]);
  }

}

//---------------------------------------------------

void display (int b[maxim], int *tob)
{

  int i;
  printf("\n\tThe items of Stack are shown...{B}\n");
  i= (*tob);
  printf("\n\n\t\t");
  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 ("\n\tThis is a Process of generating binary Code of n-bit.\n\tPlease insert n numbers in decreasing order.\n\te.g 4,3,2,1 for 4-bit binary code.\n\n");

  if (top == -1)
    printf("\n\tInitially Stack is empty...\n");
  
  printf("\n\tYou have to give data, please start insertion.\n\tPress 'y' for insert, 'n' for exit.\n\n");
  ch= getchar();
  while (ch == 'y') 
  {
    top= pushA (a, &top, &item);
    printf("\n\t'y' :insert\n\t'n' :exit.\n");
    getchar();    
    ch= getchar();
    getchar();
    if( top == (max-1) )
    {
      printf("\n\tSorry...\n\tStack is full.\n\tPush operation is not possible...\n\n");
      break;
    }
  }

  system("clear");
  displayA (a, &top);

  if(top == -1)
    printf("\n\tSorry...\n\tStack is empty...\n\tCopy is done...\n\n");
  else
   toc= cpyAC (a, c, &top);         

  tob= palgen (a, b, c, &top, &toc);      

  if(tob == -1)
    printf("\n\tSorry...\n\tStack is empty...\n\n");
  else
    display (b, &tob);

//-------------------------------------------------------

  printf("\n\tEnter the bit number of binary code: ");
  scanf("%d", &bit);

  col= bit;
  while (bit  0)
  {
    row= (row*2);
    bit --;
  }
  
  binarycode (A, &row, &col, b, &tob);
  grycddisp (A, &row, &col);

  printf ("\n\n\tT  H  A  N  K    Y  O  U\n\n");  

}
Arup Goswami
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date