Hamming reciver and sending

#include <stdio.h>

int main()
{
    int d[8],c[4],h[12],i,j,n=0,r[12],c1;
    //Sender Side
 for(i=0;i<8;i++)
 {
  printf("\nEnter %d bit of data=",i);
  scanf("%d",&d[i]);
 }
 printf("\nData entered is=\n");
 for(i=0;i<8;i++)
 printf("%d",d[i]);
 c[0]=d[0]^d[1]^d[3]^d[4]^d[6];
 c[1]=d[0]^d[2]^d[3]^d[5]^d[6];
 c[2]=d[1]^d[2]^d[3]^d[7];
 c[3]=d[4]^d[5]^d[6]^d[7];
 printf("\n Parity bit are %d %d %d %d",c[0],c[1],c[2],c[3]);
 i=0;
 for(j=0;j<12;j++)
 {
  if((pow(2,n)-1)==j)
  {
   h[j]=c[n];
   n++;
  }
  else
  {
   h[j]=d[i];
   i++;
  }
 }

 printf("\nData transmitted is=");
 for(i=0;i<12;i++)
 {
  printf("%d",h[i]);
 }


 //Receiver Side
 printf("\nEnter received code word bits one by one=");
    for(i=0;i<12;i++)
 {
  printf("\nEnter %d bit of data=",i);
  scanf("%d",&r[i]);
 }
 c[0]=r[0]^r[2]^r[4]^r[6]^r[8]^r[10];
 c[1]=r[1]^r[2]^r[5]^r[6]^r[9]^r[10];
 c[2]=r[3]^r[4]^r[5]^r[6]^r[11];
 c[3]=r[7]^r[8]^r[9]^r[10];
 c1=c[3]*8+c[2]*4+c[1]*2+c[0]*1;
 if(c1==0) 
 {
  printf("\ncongratulations there is no error: ");
 } 
 else 
 {
  printf("\nerron on the postion: %d\nthe correct message is \n",c1);
  if(r[c1]==0)
    r[c1]=1; 
  else
    r[c1]=0;
  for (i=0;i<12;i++) 
  {
   printf("%d ",r[i]);
  }
 }
    return 0;
}

Comments

Popular posts from this blog

employee