In: Computer Science
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
#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