Friday, 3 June 2016

Implementation of Hamming Code

Implementation of Hamming Code


C program

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main() {
  4. int data[7],rec[7],i,c1,c2,c3,c;
  5. printf("this works for message of 4bits in size \nenter message bit one by one: ");
  6. scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
  7. data[6]=data[0]^data[2]^data[4];
  8. data[5]=data[0]^data[1]^data[4];
  9. data[3]=data[0]^data[1]^data[2];
  10. printf("\nthe encoded bits are given below: \n");
  11. for (i=0;i<7;i++) {
  12. printf("%d ",data[i]);
  13. }
  14. printf("\nenter the received data bits one by one: ");
  15. for (i=0;i<7;i++) {
  16. scanf("%d",&rec[i]);
  17. }
  18. c1=rec[6]^rec[4]^rec[2]^rec[0];
  19. c2=rec[5]^rec[4]^rec[1]^rec[0];
  20. c3=rec[3]^rec[2]^rec[1]^rec[0];
  21. c=c3*4+c2*2+c1 ;
  22. if(c==0) {
  23. printf("\ncongratulations there is no error: ");
  24. } else {
  25. printf("\nerron on the postion: %d\nthe correct message is \n",c);
  26. if(rec[7-c]==0)
  27. rec[7-c]=1; else
  28. rec[7-c]=0;
  29. for (i=0;i<7;i++) {
  30. printf("%d ",rec[i]);
  31. }
  32. }
  33. getch();
  34. }

Output

No comments:

Post a Comment