Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.

Program to Create the Hamming Code for Given Message


#include<stdio.h>
#include<ctype.h>
#include<string.h>
int power(int,int);
void hamming();
int a[50],n;
int main()
{
        char str[20];
        int i,j,as,q,arr[10],k;
        printf("Enter any string..");
        scanf("%s",str);
        n=0;
        printf("\n\n String in binary..\n\n");
        for(j=0;str[j];j++)
        {
                i=0;
                as=toascii(str[j]);
                q=as;
                while(q!=1)
                {
                        arr[i++]=q%2;
                        q/=2;
                }
                arr[i++]=1;
                for(k=i-1;k>=0;k--)
                        a[n++]=arr[k];
        }
        printf("\n Before adding checkbits..\n");
        hamming();
        return 0;
}



void hamming()
{
        int h[10][11],i,j;
        int len=n/7,no=0,c=0,k;
        int index[4][5]={
{3,5,7,9,11}, {3,6,7,10,11}, {5,6,7,0,0}, {9,10,11,0,0}
      };
        int p[4]={1,2,4,8};
        for(i=0;i<len;i++)
        {
                for(j=1;j<=11;j++)
                {
                        if(j!=1 && j!=2 && j!=4 && j!=8)
                                h[i][j]=a[no++];
                        else
                                h[i][j]=0;
                }
        }
        for(i=0;i<len;i++)
        {
                printf("\n");
                for(j=1;j<=11;j++)
                {
                        if(j!=1 && j!=2 && j!=4 && j!=8)
                                printf("  %d",h[i][j]);
                }
        }

 
        for(i=0;i<len;i++)
        {
                for(k=0;k<4;k++)
                {
                        for(j=0;j<5;j++)
                        {
                                if(index[k][j]!=0)
                                        h[i][p[k]]=h[i][p[k]] ^ h[i][index[k][j]];
                        }
                }

        }
        printf("\n\n CodeWords \n");
        for(i=1;i<=11;i++)
                printf("  %d",i);
        printf("\n  ---------------------------------\n");

        for(i=0;i<len;i++)
        {
                printf("\n");
                for(j=1;j<=11;j++)
                        printf("  %d",h[i][j]);
        }
        printf("\n");
}

int power(int n,int c)
{
        int i,p=1;
        for(i=1;i<=c;i++)
                p=2*p;
        return p;
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes