Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 22nd, 2010, 2:24 PM   #1
thebiff
Newbie
 
Join Date: Oct 2010
Posts: 1
Rep Power: 0 thebiff is on a distinguished road
Hamming code error detection/correction

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.

#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
thebiff is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code structure lapta Delphi 1 Dec 27th, 2010 9:19 AM
Does anyone know what this line of code does? neorich2002 Other Programming Languages 3 Jul 5th, 2010 1:23 PM
weird Allocation/Deallocation error in C code Abhijit Bose C 9 Jun 14th, 2010 6:33 AM
Need help with FASM code :) NGK Assembly 0 Feb 22nd, 2010 5:24 PM
Any ideas what i've done wrong? (Newb C Programmer) Code included Ramlag C++ 10 Mar 22nd, 2005 1:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC