Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program: Objective: To check a Hamming code for a single-bit error, and to rep

ID: 3806494 • Letter: C

Question

C program:

Objective:

To check a Hamming code for a single-bit error, and to report and correct the error (if any).

Inputs: The maximum length of a Hamming code  The parity of the check bits (even=0, odd=1)  The Hamming code

Outputs: The erroneous bit (if any)  The corrected Hamming code (if there was an error) Specification:

The program checks a Hamming code for a single-bit error based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are:  1) Enter parameters  2) Check Hamming code  3) Quit program

To use the Math library, use: “#include <math.h>” to access various functions, such as pow(base, exp), log(number), etc. To perform the XOR function, use the operator “^”.
To use the String library, use: “#include <string.h>” to access various functions, such as
strlen(string) which returns an integer representing the length of a string of characters.

Explanation / Answer

Solution:-

This program is detect the error bit and correct the error by calculating the hamming distance.

---------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

{

     int a[20],b[20],c[20],d[20],i,k,m,f,n,j,r,p,x,y,z,ch,key,q,v,sum=0;

clrscr();

     printf(" ENTER THE LENGTH OF DATA WORD :");

     scanf("%d",&k);

     printf(" ENTER THE DATA WORD ");

     for(i=1; i<=k; i++)

{

           scanf("%d",&a[i]);

     }

     m=1;

     while((k+m+1)>=pow(2,m))

{

           m++;

     }

     printf(" Value of m is : %d",m);

     n=k+m;

     j=1; r=0;

     for(i=1; i<=n; i++)

{

           p=pow(2,r);

           if(i==p)

{

                b[i]=0;

                r++;

           }

           else

{

                b[i]=a[j];

                j++;

           }

     }

     printf(" INTERMEDIATE CODE WORD IS ");

     for(i=1; i<=n; i++)

{

           printf("%d",b[i]);

     p=0;

     for(i=1; i<=m; i++)

     {

           x=pow(2,p); r=1;

           for(j=x; j<=n; j=j+(x*2)){

                for(y=j; y<(j+x); y++){

                     c[r]=b[y];

                     r++;

                }

           }

           z=0;

           for(y=1; y<=(r-1); y++)

           {

                if(c[y]==1) z++;

           }

           if(z%2==0)

                b[x]=0;

           else

                b[x]=1;

           for(y=1; y<=20; y++)

{

                c[y]=0;

           p++;

}

. }

}

     printf(" THE HAMMING CODE IS ");

     for(i=1; i<=n; i++)

{

           printf("%d",b[i]);

     while(1)

{

printf(" PRESS 1 TO ALTER A BIT 0 to EXIT ");

                scanf("%d",&ch);

                if(ch==1)

{

printf(" ENTER THE BIT YOU WANT TO CHANGE ");

                        scanf("%d",&key);

                        for(i=1; i<=n; i++){

                                if(i==key){

                                        if(b[key]==1) b[key]=0;

                                        else b[key]=1;

                                        break;

                                }

                        }

                        printf(" THE NEW CODE IS ");

                        for(i=1; i<=n; i++)

                                printf("%d",b[i]);

                }

                else

                        break;

        }

     p=0; q=0;

        for(i=1; i<=m; i++)

        {

                x=pow(2,p); r=1;

                for(j=x; j<=n; j=j+(x*2)){

                        for(y=j; y<(j+x); y++){

                                c[r]=b[y];

                                r++;

                        }

                }

                z=0;

                for(y=1; y<=(r-1); y++)

                {

                        if(c[y]==1) z++;

                }

                v=z%2;

           d[q]=v;

           sum=sum+(v*pow(2,q));

           q++;

                for(y=1; y<=20; y++)

                        c[y]=0;

                p++;

        }

}

     if(sum==0)

           printf(" NO ERROR FOUND....... ");

     else

           printf(" ERROR AT POSITION %d",sum);

     printf(" ");

     return 0;

getch();

}

---------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------