Hi I am doing a project about hamming code, the input is the length of the hamming code and if the parity is even or odd. I am getting a few errors that I cannot figure out how to fix.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*global vars */
int length;
int parity;
char *hamming_string=NULL;
void enter_params(){
/* declare local vars */
/* prompt for length of hamming code, parity */
printf("Enter the length of the hamming code: ");
scanf("%d", &length);
printf("Enter the parity (0=even, 1-odd): ");
scanf("%d", &parity);
/* alocate space for hamming code */
hamming_string=(char*)malloc(length*sizeof(char));
return;
}
void check(){
/* declare local vars */
int i;
int j;
int k;
int start;
int step;
int parity_check;
int error_bit;
int num_parity_bits;
printf("Enter the Hamming code: ");
scanf("%s", hamming_string);
num_parity_bits=ceil(log(length)/log(2));
for(i=0; i<num_parity_bits; i++){
parity_check=parity;
error_bit=0;
start=pow(2,i);
step=2*start;
error_bit+=parity_check*start;
for(j=start; j<length; j=j+step)
for(k=j; k<start+step&&k<length; k++)
parity_check=parity_check^(hamming_string[length-k]-'0');
}
if(error_bit != 0){
if(hamming_string[length-error_bit]=='0'){
printf("There is an error in bit: %d", hamming_string[length-error_bit]=='0');
hamming_string[length-error_bit]='1';
printf("\n");
}
else{
hamming_string[length-error_bit]='0';
}
printf("The corrected Hamming code is: %s", hamming_string);
printf("\n");
}
else{
printf("There is no bit error");
printf("\n");
}
return;
}
/* void exit(){
if(hamming_string!=NULL){
free(hamming_string);
}
}*/
int main(){
int choice = 0;
while(choice!=3){
printf("Error detection/correction:\n");
printf("----------------------\n");
printf("1) Enter parameters\n");
printf("2) Check Hamming code\n");
printf("3) Exit\n");
printf("\n Enter Selection: ");
scanf("%d", &choice);
if(choice==1){
enter_params();
printf("\n");
main();
}
else if(choice==2){
check();
printf("\n");
main();
}
/*else if(choice==3){
exit();
}*/
return 1;
}
}
when i input the length as 7 and the parity as 0 , then the hamming code as 1000110.
i am getting the output that there is no error, when i should get there is an error in bit 6 and the corrected hamming code should be 1100110.
then when i input the length as 7 and the parity as 1, and then the hamming code as 1000110. i get the correct error bit as 1 but when it corrects it i get 1001110 instead of 1000111.
any help would be much appreciated.
Thanks