Hamming Code implementation using C programming language.

#include<stdio.h>
#include<math.h>
int check(double b,int p,int a[],int c)
{
    int bloc[]={0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111};
    int i,l=0;
    printf("\nCheck\n");
    for(i=1;i<=c;i++)
    {
        if(bloc[i]%(int)b>=(int)b/10)
        {
            printf("loc  %d\tBit - %d\n",i,a[i]);
            if(a[i]==1)
            {
                l++;
            }
        }
    }
    if(l%2==0)
    {
        if(p==1)
            return 1;
        else
            return 0;
    }
    else
    {
        if(p==1)
            return 0;
        else
            return 1;
    }
}
int main()
{
    int mb[1000],i=1,d,count=0,k=1,j,p,loc=0,c[40],q;
    double b;
    printf("Enter '1' for odd parity and '2' for even parity : ");
    scanf("%d",&p);
    printf("Enter the recieved binary code  : ");
    scanf("%lf",&b);
    while(b!=0)
    {
        d= (int)b%10;
        mb[i]=d;
        i++;
        b=(int)b/10;
        count++;
    }
    printf("\n\n");
    for(i=1;;i++)
    {
        if(pow(2,i)>=count)
        {
            q=i;
            break;
        }
    }
    for(i=0;i<q;i++)
    {
        k=k*10;
        j=pow(2,i);
        c[j]=check(k,p,mb,count);
        printf("C%d => %d \n ",j,c[j]);
    }
    for(i=0;i<q;i++)
    {
        j=(pow(2,i));
        loc=loc+c[j]*j;
    }
 
    printf("\n\nLocation Of Error is : %d",loc);
    printf("\nCorrect Message is : ");
    for(i=count;i>0;i--)
    {
        if(i==loc)
        {
            if(mb[i]==0)
                printf("1");
            else
                printf("0");
        }
        else
            printf("%d",mb[i]);
    }
    printf("\n\n\n");
}

Comments

Popular posts from this blog

CS50 IDE (Best Platform for coding Online/Offline)

Best Apps to do Programming on Android Platform.

Fun Games that runs on C programming language