[text] kjvdskj

Viewer

  1. /*Problem defination: Write a program to find Hamming code of given data bits. 
  2. If one of the Hamming code bit is corrupted then find error bit location and correct error accordingly*/
  3.  
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<math.h>
  8.  
  9. void main()
  10. {
  11.     int maxp=6;
  12.     int a[50],temp[70],temp2[70];
  13.     int t,i,j,k,nd,n,nh,sum=0,pos=0;
  14.  
  15.     printf("Enter Length of Data String: ");
  16.     scanf("%d",&nd);
  17.     printf("Enter Data String: ");
  18.     for(i=0;i<nd;i++)
  19.     {
  20.         scanf("%d",&a[nd-i-1]);
  21.     }
  22.     printf("-----------------------------------\n",nd);
  23.     for(i=0,j=0;i<nd;i++)
  24.     {
  25.         for(k=0;k<maxp;k++)
  26.         {
  27.             t=pow(2,k)-1;
  28.             if(j==t)
  29.             {
  30.                 temp[j]=0;
  31.                 j++;
  32.             }
  33.         }
  34.         temp[j]=a[i];
  35.         j++;
  36.     }
  37.     nh=j;
  38.     printf("Length of Hamming code: %d bits\n",nh);
  39.     n=nh-nd;
  40.     printf("Number of Parity Bits: %d \n",n);
  41.  
  42.     int b[n];
  43.     int m=n-1;
  44.     for(k=0;k<n;k++)
  45.     {
  46.         t=pow(2,k)-1;
  47.  
  48.         for(i=t;i<nh;)
  49.         {
  50.             for(j=0;j<=t;j++)
  51.             {
  52.                 sum=sum+temp[i];
  53.                 i++;
  54.                 if(i>=nh)
  55.                     break;
  56.             }
  57.  
  58.             if(i>=nh)
  59.                 break;
  60.  
  61.             for(j=0;j<=t;j++)
  62.             {
  63.                 i++;
  64.                 if(i>=nh)
  65.                     break;
  66.             }
  67.  
  68.             if(i>=nh)
  69.                 break;
  70.         }
  71.         temp[t]=sum%2;
  72.         sum=0;
  73.         printf("P%d: %d\n",t+1,temp[t]);
  74.     }
  75.  
  76.  
  77.     printf("\nHamming code: Sender side:   ");
  78.     for(i=0;i<nh;i++)
  79.     {
  80.         printf("%d ",temp[nh-i-1]);
  81.     }
  82.  
  83.  
  84.     printf("\nHamming code: Receiver side: ");
  85.     for(i=0;i<nh;i++)
  86.     {
  87.         scanf("%d",&temp2[i]);
  88.     }
  89.     sum=0;
  90.     for(k=0;k<n;k++)
  91.     {
  92.         t=pow(2,k)-1;
  93.  
  94.         for(i=t;i<nh;)
  95.         {
  96.             for(j=0;j<=t;j++)
  97.             {
  98.                 sum=sum+temp2[i];
  99.                 i++;
  100.                 if(i>=nh)
  101.                     break;
  102.             }
  103.  
  104.             if(i>=nh)
  105.                 break;
  106.  
  107.             for(j=0;j<=t;j++)
  108.             {
  109.                 i++;
  110.                 if(i>=nh)
  111.                     break;
  112.             }
  113.  
  114.             if(i>=nh)
  115.                 break;
  116.         }
  117.         b[m]=sum%2;
  118.         sum=0;
  119.         printf("P%d: %d\n",t+1,b[m]);
  120.         m--;
  121.     }
  122.     for(m=0;m<n;m++)
  123.     {
  124.         pos=pos+b[n-m-1]*pow(2,m);
  125.     }
  126.     printf("Position of Error: %d\n",pos);
  127.     if(temp2[pos-1]==0)
  128.         temp2[pos-1]=1;
  129.     else
  130.         temp2[pos-1]=0;
  131.  
  132.     printf("\nHamming code: Receiver side: Error Corrected:  ");
  133.     for(i=0;i<nh;i++)
  134.     {
  135.         printf("%d ",temp2[i]);
  136.     }
  137.  
  138.     printf("\n-----------------------------------\n",nd);
  139. }

Editor

You can edit this paste and save as new:


File Description
  • kjvdskj
  • Paste Code
  • 02 Jun-2023
  • 2.8 Kb
You can Share it: