Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
#include <stdio.h>
#include <stdlib.h>
/* declare global var's */
int hamming_length=0, parity_type=-1;
/* declare & initialize hamming string */
char *hamming = NULL;
/* Option #1 */
void params() {
printf("Enter the length of the Hamming code: ");
scanf("%d", &hamming_length);
printf("Enter the parity (o=even, 1=odd): ");
scanf("%d", &parity_type);
hamming = (char *) malloc (hamming_length * sizeof(char));
}
/* Option #2 */
void check_hamming_code() {
/* declare local var's */
int i,j,k, error_bit=0;
/* prompt for hamming code string */
printf("Enter the Hamming code: ");
scanf("%s",hamming);
for(i=1; i < hamming_length; i*=2) {
int current_parity = parity_type;
for(j=i; j <= hamming_length; j+=2*i) {
for(k=j; (k <= hamming_length) && (k < i+j); k++) {
if(k != i)
current_parity = current_parity ^ (hamming[hamming_length-k]-'0');
} /* end of k loop */
} /* end of j loop */
error_bit = error_bit + (current_parity ^ (hamming[hamming_length-i]-'0'))*i;
} /* end i loop */
/* correct error bit */
if(error_bit == 0)
printf("There is no bit error\n");
else {
printf("There is an error in bit: %d\n",error_bit);
if((hamming[hamming_length - error_bit]-'0') == 0) {
hamming[hamming_length - error_bit] = (hamming[hamming_length-error_bit] +1);
}
else {
hamming[hamming_length - error_bit] = (hamming[hamming_length-error_bit] -1);
}
printf("The corrected Hamming code is: %s\n", hamming);
}
}
/* Option #3 */
void exit_program() {
if(hamming != NULL)
free(hamming);
}
int main() {
/* declare local var's */
int menu_option = 0;
while(menu_option != 3) {
printf("\n");
printf("Error detection/correction:\n");
printf("------------------------------------\n");
printf("1) Enter parameters\n");
printf("2) Check Hamming code\n");
printf("3) Exit\n");
printf("\n");
printf("Enter selection: ");
scanf("%d",&menu_option);
printf("\n");
switch (menu_option) {
case 1: params(); break;
case 2: check_hamming_code(); break;
case 3: exit_program(); break;
default: break;
} /* exit switch statement*/
} /* exit while statement*/
return 0;
}
1.png
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
void params() {
printf("Enter the length of the Hamming code: ");
scanf("%d", &hamming_length);
printf("Enter the parity (o=even, 1=odd): ");
scanf("%d", &parity_type);
hamming = (char *) malloc ((hamming_length + 1) * sizeof(char)); // <------------- !
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.