CSE 307 : Hamming Code

Friends,

Here is the code for the hamming code.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int i=0;
int main()
{
    int a,ab[20],b[20],c,j,r;
    printf(“Enter the binary  data”);
    scanf(“%d”,&a);
    c=a;
    while(a>0)                       //for collectiong the bits
    {
              ab[i]=a%10;
              a=a/10;
              ++i;
    }
    printf(“\nthe number of data bit is%d\n”,i);   
    for(j=0;j<i;j++)    //for storing in correct order that is the way in which it was entered
    b[j]=ab[i-j-1];
    for(j=0;j< i;j++)    //printing the data bits entered
    printf(“%d”,b[j]);
    for(j=0;j<=2*i;j++)  //finding the redundancy bits
    {
             if(pow(2,j)>=i+j+1)
            {
                     r=j;
                      break;
           }
    }


     printf(“\nNumber of redundency bits is %d\n “,r);
     int q= i+r;
     int arr[q],n,s=0;
    long t;
     j=0;
     for(n=1;n<=i+r;n++)   //now combining the redundancy bits and data bits
      {
                 t=pow(2,s);
                if(t==n )
                 {      arr[n-1]=0;
                         s++;
                  }
                   else
                   {      
                           arr[n-1]=b[j];
                           ++j;
                    }
          }
         printf(“The combination of data and redundancy bits\n”);      
         for(n=0;n<i+r;n++)//printing combination of data and redundancy bits
          printf(“%d”,arr[n]);    
          int bit[r],sum=0,sum1=0,sum2=0,sum3=0;  
          s=0;               
         for(n=1;n<=i+r;n++)        //logic for calculating redundancy bits and inserting with data bits
           {
                      t=pow(2,s);
                     if(t==n )
                    {  
                               s++;
                    }
                  else
                   {   
                         if(arr[n-1]==1 )
                         {
                                   if(arr[n-1]==1 && n%2!=0) //for redundancy bit 1
                                    sum=sum+1;
                                    if(n==3 ||n==6 ||n==7 || n==10 ||n==11 ||n==14 ||n==15)//for r2
                                    sum1=sum1+1;
                                    if(n==5 ||n==6 ||n==7 || n==12 ||n==13 ||n==14 ||n==15)//for r3
                                     sum2=sum2+1;
                                      if(n>8)//for r4
                                       sum3=sum3+1;
                              }
                         }
                }  
                 if(sum%2==0)//using even parity
                   bit[0]=0;
                    else
                   bit[0]=1;
                   if(sum1%2==0)
                     bit[1]=0;
                      else
                     bit[1]=1;
                      if(sum2%2==0)
                  bit[2]=0;
                      else
                       bit[2]=1;
                      if(sum3%2==0)
                       bit[3]=0;
                       else
                      bit[3]=1;
                       printf(“\n”);
                       printf(“\nThe redudancy bits are\n”);
                        for(j=0;j<r;j++)
                                printf(“%d\n”,bit[j]);
                        arr[0]=bit[0];
                        arr[1]=bit[1];
                         arr[3]=bit[2];
                         arr[7]=bit[3];
                         printf(“The transmitted data after doing calulation from LHS  and even parity method\n”);
                        for(n=0;n<i+r;n++)
                                    printf(“%d”,arr[n]);
                       int rec[q];
                       printf(“\nEnter the recived data”);
                       for(n=0;n<q;n++)
                       scanf(“%d”,&rec[n]);
                       j==1;
                      for(n=0;n<i+r;n++)
                      {      ++j;
                               if(arr[n]!=rec[n])
                               {
                                         printf(“\nThe error is in %d bit”,n+1);
                                          break;
                               }
                       }
                       if(j==q)
                       printf(“\nTheir is no error”);
                        getch();
                       return 0;
           }

Thank you
                              
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s