DCP10
DCP10
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
// msg: 1 0 1 0
// received: 1 0 1 0 1 1 0
int main()
{
//BIT INDEXING-right to left
int m,r=0,msg[50],data[60],i,j,k;
printf("Enter message size: ");
scanf("%d",&m);
//calculate no of r bits
while(1)
{ //keep incrementing UNTIL 2^r >= m+r+1 satisfied
if((m+r+1)<=(int)pow(2,r))
break;
r++;
}
//input message
printf("Enter message bits (space separted): ");
for(i=m;i>=1;i--)//backwards because reverse indexing
{
scanf("%d",&msg[i]);
}
//filling the spaces to generate codeword
k=0; //for 2^k / position of r bits
j=1; //for position of msg bits
for(i=1;i<=(m+r);i++)
{
if(i==(int)pow(2,k)) //finding redundant bit positions
{ //at positions with powers of 2, put number 8 as a representation of r/empty
data[i]=8;
k++;
}
else
{ //at other positions, put message bits as they were
data[i]=msg[j];
j++;
}
}
printf("\n---x---x---x---x---x---x---x---x---x---x---x---x---x---x---x---x---\n");
return 0;
}
INPUT / OUTPUT:
Hamming code does not work for error in more than 1 bits.