Thread: new to C & need help Hamming code error detection

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    new to C & need help Hamming code error detection

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    /*global vars */
       int length;
       int parity;
       char *hamming_string=NULL;
    
        int 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;
       }
    
        int check(){
       /* declare local vars */
          int i;
          int j;
          int k;
     
          int parity_check;
          int error_bit;
    
          printf("Enter the Hamming code: ");
          scanf("%s", hamming_string);
    
          for(i=1; i<length; i = i*2){
             parity_check=parity;
             error_bit=0;
      
             for(j=i; j<length; j=j + 2*i){
                for(k=j; k<i+2*i&&k<length; k++){
                   if(k != i){
    		parity_check = parity_check ^ (hamming_string[length - k] - '0');}
    					}
    				}
    					
    	error_bit= error_bit + (parity_check ^ (hamming_string[length - i] - '0')*i);
    
    			
          }
          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;
       }
    
    
    
        int quit(){
          if(hamming_string!=NULL){
             free(hamming_string);
          }
       }
    
    
    	int main()
    {
    	int interface = 0;
    	while(interface != 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", &interface);
    		}
    	
    		switch(interface)
    		{
    			case 1: enter_params(); break;
    			case 2: check(); break;
    			case 3: quit(); break;
    			default: break;
    		}
    	}
    		return 1;
    }

    Hi, I enter length 7 for the Hamming code and enter 0 for Parity.

    When I choose selection 2, I enter 1000110 for the Hamming code and I get a message that there is no bit error. The answer I am supposed to get is there is an error in bit 6 and the corrected Hamming code is 11000110.

    the funny thing is that I get the correct answer when I enter 1 for Parity and the same length (7) and Hamming code(1000110). The correct answer for that problem is error in bit 1 and the corrected Hamming code is 1000111

    thank you for your help!

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. You declared variable parity as a global.
    2. You are only initializing it in enter_Parameters() function. ( whatever name given by you for choice 1).
    3. Suppose, user enters choice 2 in the very first time, parity variable will not be initialized and; from here it depends upon the machine you are using.
    a). either your compiler assigns 0 to all non initialized variables.
    b). either it leaves the non initialized variables to keep garbage...
    Check this thing over your machine and do some necessary measures that could solve your problem.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Code:
     int check(){
       /* declare local vars */
          int i;
          int j;
          int k;
     
          int parity_check;
          int error_bit;
    
          printf("Enter the Hamming code: ");
          scanf("%s", hamming_string);
    		
    		
          for(i=1; i<length; i = i*2){
             parity_check=parity;
    	error_bit=0;
    
             
      
             for(j=i; j<length; j=j + 2*i){
                for(k=j; k<i+2*i&&k<length; k++){
                   if(k != i){
    		parity_check = parity_check ^ (hamming_string[length - k] - '0');}
    					}
    				}
    					
    	error_bit = error_bit + (parity_check ^ (hamming_string[length - i] - '0')*i);
    
    			
          }
          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("The error bit is: %d", error_bit);
             printf("There is no bit error");
             printf("\n");
          }
           
           
          return;
       }
    my error_bit does not seem to update. The error_bit variable seems to stay zero. Is there a problem with where the error_bit variable is declared and initialized to zero?

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    This time initialization and declarations are right but one thing that i couldn't understand is,
    your variables parity and parity check are static single variables and why do you need to assign parity to parity_check every time inside the loop??
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your indentation makes it really hard to see what's going on.
    Code:
    int check(){
        /* declare local vars */
        int i;
        int j;
        int k;
        int parity_check;
        int error_bit;
    
        printf("Enter the Hamming code: ");
        scanf("%s", hamming_string);
    
        for(i=1; i<length; i = i*2){
            parity_check=parity;
            error_bit=0;
    
            for(j=i; j<length; j=j + 2*i){
                for(k=j; k<i+2*i&&k<length; k++){
                    if(k != i){
                        parity_check = parity_check ^ (hamming_string[length - k] - '0');
                    }
                }
            }
    
            error_bit = error_bit + (parity_check ^ (hamming_string[length - i] - '0')*i);
        }
    
        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("The error bit is: %d", error_bit);
            printf("There is no bit error");
            printf("\n");
        }
    
        return;
    }
    See SourceForge.net: Indentation - cpwiki

    > for(i=1; i<length; i = i*2)
    Two things,
    a) why are you starting at 1, when arrays start at 0?
    b) why *2? You have the sequence 1,2,4,8,16
    You seem to be missing large chunks of data
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM