0% found this document useful (0 votes)
135 views

Hamming Code PGM

This C program implements Hamming code for error detection and correction. It allows the user to choose to either encode 4 data bits into 7 encoded bits using a generator matrix or decode 7 received bits to detect and correct errors using a parity check matrix. The user inputs the data bits or encoded bits and the program outputs the encoded bits, detects and corrects errors, or indicates the data is error free.

Uploaded by

Praveen Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Hamming Code PGM

This C program implements Hamming code for error detection and correction. It allows the user to choose to either encode 4 data bits into 7 encoded bits using a generator matrix or decode 7 received bits to detect and correct errors using a parity check matrix. The user inputs the data bits or encoded bits and the program outputs the encoded bits, detects and corrects errors, or indicates the data is error free.

Uploaded by

Praveen Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program for hamming code generation for error detection and correction

Program-name: ham.c
#include<stdio.h>
#include<unistd.h>
#include<math.h>
int main()
{
int i,j;
char data[4];
int smatrix[4][7]={0,1,1,1,0,0,0,
1,0,1,0,1,0,0
1,1,0,0,0,1,0
1,1,1,0,0,0,1};
int encode[7]={0};
int edata[7]={0};
int errdig;
int syn[3]= {0,0,0};
int hmatrix[3][7] = { 1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1
};
int choice;
for(;;)
{
printf("\n 1-ENCODE ");
printf("\n 2-DECODE ");
printf("\n 3-EXIT ");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter 4 data bits\n");
scanf("%s",data);
for(i=0;i<7;i++)
for(j=0;j<4;j++)
encode[i]=encode[i]+(data[j]*smatrix[j][i]);
printf("\n The encoded data is\n\n");
for(i=0;i<7;i++)
{
encode[i]=encode[i] % 2;
}

for(i=0;i<7;i++)
printf("%d",encode[i]);
break;
case 2:
printf("\n Enter the encoded data \n");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
for(j=0;j<7;j++)
syn[i]=syn[i] + ( rmatrix[i][j] * edata[j] );
for(i=0;i<3;i++)
syn[i]=syn[i] % 2;
errdig = 4 * syn[0] + 2 * syn[1] + 1 * syn[2];
if (errdig == 0)
printf("\n Error free data \n");
else
{
printf("\n Error in the bit number %d\n",errdig);
errdig--;
if(edata[errdig] == 1)
edata[errdig]=0;
else
edata[errdig]=1;
}
printf("\n The actual data error free data : ");
for(i=3;i<7;i++)
printf("%d",edata[i]);
break;
default : exit(0);
}
}
getch( );
}

Output
Run1:
1Encode
2Decode
3Exit
Enteryourchoice:1
Enterthe4databits
1010
Theencodeddatais1011010
Run2:
1Encode
2Decode
3Exit
Enteryourchoice:2
Entertheencodeddata1011010
Errorfreedata
Theactualerrorfreedata:1010
Run3:
1Encode
2Decode
3Exit
Enteryourchoice:2
Entertheencodeddata1011011
Errorinbitnumber7
Theactualerrorfreedata1011

You might also like