Question

In: Computer Science

Error detection/correction C Objective: To check a Hamming code for a single-bit error, and to report...

Error detection/correction C

Objective:

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

Inputs:

1.The maximum length of a Hamming code

2.The parity of the check bits (even=0, odd=1)

3.The Hamming code as a binary string of 0’s and 1’s

Outputs:

1.The original parity bits (highest index to lowest index, left to right)

2.The new parity bits (highest index to lowest index, left to right)

3.The bit-wise difference between the original parity bits and the new parity bits

4.The erroneous bit (if any)

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

If necessary, include the flag “-lm” when you compile i.e. gcc filename.c–lm to be able to utilize the math library.

skeleton:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <string.h>

/* declare global var's including a dynamic array of characters to store

the Hamming code,

original parity bits, and new parity bits*/

/*************************************************************/

void "OPTION 1"()

{

/* prompt for maximum length of hamming code and even/odd parity */

printf("\nEnter the maximum length of the Hamming code: ");

/* allocate memory for Hamming code */

return;

}

/*************************************************************/

void "OPTION 2"()

{

/* declare local var's */

/* prompt for Hamming code */

/* calculate actual length of input Hamming code, number of parity bits,

and highest parity bit */

/* Allocate memory for original and new parity bits */

/* Map parity bits within Hamming code to original parity bit array */

/* Calculate new parity bits */

/* OUTER LOOP: FOR EACH PARITY BIT */

/* initialize parity bit to even/off parity */

/* MIDDLE LOOP: FOR EACH STATING BIT OF A CONSECUTIVE SEQUENCE */

/* INNER LOOP: FOR EACH BIT OF A SEQUENCE TO BE CHECKED */

/* ignore original parity bit */

/* update new parity bit value based on Hamming code bit

checked */

} /* END INNER LOOP */

/* Map new parity bit value to new parity bit array */

} /* END OUTER LOOP */

/* Calculate error bit by XORing original and new parity bits from

respective arrays, weighted properly */

/* Print original parity bits & new parity bits and bit-wise difference */

/* If error, correct the bit and print which bit is in error and corrected

Hamming code */

/* Else if no error, print message of no code bit error */

return;

}

/******************************* OPTIONAL ************************/

void "FREE MEMORY"()

{

/* If daynamic array representing Hamming code is not NULL, free the

memory */

return;

}

/*****************************************************************/

int main()

{

/* print menu of options, select user's choice, call appropriate

procedure, and loop until user quits */

return 1;

}

Please do not use abcdef as variable names

Solutions

Expert Solution

#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("\n** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", 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 \n");
        }
        else
        {
            printf("There is an error in bit: %d\n", ErrorBit);
            if(HammingString[length - ErrorBit] == '0')
            {
                HammingString[length - ErrorBit] = '1';
            }
            else
            {
                HammingString[length - ErrorBit] = '0';
            }

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

    int main()
    {

        int parity;
        int choice = 0;
            printf("Error detection/correction: \n");
            printf("----------------------------\n");
            printf("1) Enter parameters \n");
            printf("2) Check Hamming code \n");
            printf("3) Exit \n");
            printf("\nEnter 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. \n");
                    exit(0);
                }   
            }//end while
            exit(0);
    }//end main


Related Solutions

Two communication devices are using a single-bit even parity check for error detection. The transmitter sends...
Two communication devices are using a single-bit even parity check for error detection. The transmitter sends the byte 10101010 and, because of channel noise, the receiver gets the byte 10011010. Will the receiver detect the error? Why or why not? the receiver gets the byte 10111010. Will the receiver detect the error? Why or why not? Compute the Internet checksum for the data block E3 4F 23 96 44 27 99 D3. Then perform the verification calculation.
For the following Hamming Code word, please indicate the location of the bit in error (assuming...
For the following Hamming Code word, please indicate the location of the bit in error (assuming even parity): 0011 0001 010
5. Implement 2-D error detection mechanism using even parity check. Given a data bit to be...
5. Implement 2-D error detection mechanism using even parity check. Given a data bit to be send, perform the even parity check for the data to be send. Append the parity bit at the end of that data. At the receiver end, the receiver has to check for errors in transmission Please code in C language.
You and a friend are using the C(7,4) Hamming code to send some 4-bit messages to...
You and a friend are using the C(7,4) Hamming code to send some 4-bit messages to each other. (a) You encode the message 1010 and send the encoded 7-bit sequence to your friend, who receives 1011011. How many errors were introduced during transmission? (b) You subsequently receive the encoded sequence 0111011 from your friend. Assuming at most one error, what is the 4-bit message that your friend sent?
Generate a transmitted dada sequence for “101010” using Hamming code? If an error occurs in the...
Generate a transmitted dada sequence for “101010” using Hamming code? If an error occurs in the third bit, the received data is 101011”, how would Hamming technique fix it?
How can the Hamming error correcting code be improved? what are the improvements and implications?
How can the Hamming error correcting code be improved? what are the improvements and implications?
A (7, 4) error correcting Hamming code is designed with a generator matrix, G = ...
A (7, 4) error correcting Hamming code is designed with a generator matrix, G =   1 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 1 1 1 1   . (1) a) (10 points) Determine the code word for the message, 0110. b) (10 points) Determine the code word for the message, 1100. c) (9 points) Determine the syndrome vector for the...
The ECG machine is reading “ERROR—Check lead placement.” Which of the following requires correction? Group of...
The ECG machine is reading “ERROR—Check lead placement.” Which of the following requires correction? Group of answer choices V6 at left midaxillary line at level of V4 horizontally. V5 at left anterior axillary line at level of V4 horizontally. V2 at fifth intercostal space midclavicular line. V1 at fourth intercostal space at right sternal angle.
Construct a generator matrix and a parity check matrix for a ternary Hamming code Ham(2, 3)....
Construct a generator matrix and a parity check matrix for a ternary Hamming code Ham(2, 3). Assume a codeword x from for the ternary Hamming code Ham(2, 3)$ was sent and the word y was received. Use the partiy check matrix you constructed in part (a) to decode y in each part using syndrome decoding: (b) y = ( 1 , 1 , 1 , 0 ), (c) y = ( 2 , 2 , 2 , 2 ), (d)...
write and explain two dimensional parity check code and hamming codes in simple way please
write and explain two dimensional parity check code and hamming codes in simple way please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT