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

Use C program: Objective: --->To check a Hamming code for a single-bit error, an

ID: 3812216 • Letter: U

Question

Use 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.

PLEASE MAKE SURE THE POGRAM RUNSSSSSS.

TEST CASES ARE:

1
12
0
2
1000110
1
21
1
2
1000110
2
1000111
3

Explanation / Answer

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

    /** Initializing the global variables */
    int MaxLength;
    int length;
    int parity;
    // Initialize the hamming string with a random or NULL memory address
    char *HammingString=NULL;

    /** Function to enter the values */
    void EnterParameters(int *length, int *parity)
    {
        printf("Enter the maximum length: ");
        /** %d reads an integer to be stored in an int. This integer can be signed */
        scanf("%d", length);
        printf("Enter the parity (0=even, 1=odd): ");
        /** %d reads an integer to be stored in an int. This integer can be signed */
        scanf("%d", parity);
    }

    void CheckHamming(char *HammingString, int parity)
    {
        // Initializing the local variables i, j, k, start, length, ParityNumber
        int i, j, k, start, length, ParityNumber;
        printf("Enter the Hamming code: ");
        scanf("%s", HammingString);

        int ErrorBit = 0;                        // Initialize the error bit
        length = strlen(HammingString);          // The strlen computes the length of a string up to, but not including the terminating null character
        length--;
        if (length > MaxLength)
        {
            printf(" ** Invalid Entry - Exceeds Maximum Code Length of %d ", MaxLength);
            return;
        }
        ParityNumber = ceil(log(length)/log(2)); // The ceil function returns the smallest integer that is greater than or equal to 'x'.

        for(i = 0; i < ParityNumber; i++)
        {
            // pow returns x raised to the power y. In this case, 2 raised to the power i.
            start = pow(2, i);
            int ParityCheck = parity;

            for(j = start; j < length; j=j+(2*start))
            {
                for(k = j; (k < ((2*j) - 1)) && (k < length); k++)
                {
                    ParityCheck ^= (HammingString[length - k] - '0');
                } // End the k for-loop
            } // End the j for-loop

                ErrorBit = ErrorBit + (ParityCheck * start);
            } // End the i for-loop

        if(ErrorBit == 0)
        {
            printf("No error ");
        }
        else
        {
            printf("There is an error in bit: %d ", ErrorBit);
            if(HammingString[length - ErrorBit] == '0')
            {
                HammingString[length - ErrorBit] = '1';
            }
            else
            {
                HammingString[length - ErrorBit] = '0';
            }

            printf("The corrected Hamming code is: %s ", HammingString);
        }
    } // End CheckHamming

    int main()
    {

        int parity;
        int choice = 0;
            printf("Error detection/correction: ");
            printf("---------------------------- ");
            printf("1) Enter parameters ");
            printf("2) Check Hamming code ");
            printf("3) Exit ");
            printf(" Enter selection: ");
            scanf("%d", &choice);

            while (choice != 3)
            {
                if (choice == 1)
                {
                    EnterParameters(&MaxLength, &parity);
                    HammingString = (char*) malloc (MaxLength * sizeof(char));
                    main();
                }
                else if (choice == 2)
                {
                    CheckHamming(HammingString, parity);
                    main();
                }
                else
                {
                    printf("Valid options are 1, 2, or 3. Quitting program. ");
                    exit(0);
                }   
            }//end while
            exit(0);
    }//end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote