9 Replies - 24419 Views - Last Post: 18 October 2012 - 10:22 PM Rate Topic: -----

#1 jldavis76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-September 11

Hamming Code program

Posted 28 March 2012 - 03:39 PM

Hello,

For an assignment, we have to write a program that takes a Hamming code as input and outputs if there is an error, and then corrects the error. Once I compile the program, I keep getting either a segmentation fault or it just gets stuck in a loop and does not do anything. The code I have so far:

#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>

void check_hamming(char *hamming_string, int parity){

  int length, num_parity, i, j, start, k;

  printf("Enter the Hamming code: ");
  scanf("%s",hamming_string);

  int error_bit = 0;
  length = strlen(hamming_string);
  num_parity = ceil(log(length)/log(2));

  for(i = 0; i < num_parity; i++){
    start = pow(2, i);
    int check_parity = parity;

    for(j = start; j < length; j=(2*start)){
      for(k = j; (k < ((2*j) - 1)) && (k < length); k++){
        check_parity ^= (hamming_string[length - k] - '0');
      }//end for k
    }//end for j

    error_bit = error_bit + (check_parity*start);
  }//end for i

  if(error_bit == 0)
    printf("No error \n");
  else{
    printf("There is an error in bit: %d", error_bit);
    if(hamming_string[length - error_bit] == '0')
      hamming_string[length - error_bit] = '1';
    else
      hamming_string[length - error_bit] = '0';

    printf("The corrected Hamming code is: %s \n", hamming_string);
  }//end else
}//end check_hamming


I also have a main, as well and a function to enter the parameters, but I don't think those are the problems since everything works fine until I enter the hamming code. Let me know if it would help if I posted them. Any help would be greatly appreciated!

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Hamming Code program

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Hamming Code program

Posted 28 March 2012 - 03:42 PM

Post a complete program, trying to find a segmentation fault without a complete program is difficult.

Have you run your program through your debugger? You debugger should be able to show where it is detecting the problem. And you should then be able to evaluate the variables at the time of the crash.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 jldavis76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-September 11

Re: Hamming Code program

Posted 28 March 2012 - 03:46 PM

Thanks for the reply Jim. Here are the other files:

Main.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main(){
	
	char *hamming_string = NULL;
	int parity, max_length;
	int choice = 0;
	
	while( choice != 5){
		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);
		
		switch(choice){
		
			case 1: enter_params(&max_length, &parity);
				hamming_string = (char*) malloc (max_length * sizeof(char));
				break;
			case 2: check_hamming(hamming_string, parity);
				break;
			case 3: quit_program(hamming_string);
				break;
		}//end switch		
	}//end while
	
	return 1;
}//end main


enter_params.c:
#include<stdio.h>

void enter_params(int *max_length, int *parity){

	printf("Enter the maximum length: ");
	scanf("%d", &max_length);
	printf("Enter the parity (0=even, 1=odd): ");
	scaf("%d", &parity);
}


quit_program.c
#include<stdlib.h>

void quit_program(char *hamming_string){
	if( hamming_string != NULL)
		free(hamming_string);
}


And you will have to forgive me, but I'm pretty new to C, and I'm not sure how to use a debugger. I use the gcc compiler.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Hamming Code program

Posted 28 March 2012 - 04:10 PM

In the following function:
void enter_params(int *max_length, int *parity){

	printf("Enter the maximum length: ");
	scanf("%d", &max_length);
	printf("Enter the parity (0=even, 1=odd): ");
	scanf("%d", &parity);
}


Your scanf() calls seem incorrect. You should not need the ampersands because the variables are already pointers.

Please show a sample of your inputs.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 jldavis76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-September 11

Re: Hamming Code program

Posted 28 March 2012 - 04:22 PM

You are a genius! So, that fixed the segmentation fault, but it seems I have problems with the logic of the code, so now I have to fix that! Hopefully, that will be easier. Thank you for help!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Hamming Code program

Posted 28 March 2012 - 04:50 PM

Quote

You are a genius!

Actually my compiler pointed this problem out:

Quote

main.c||In function ‘enter_params’:|
main.c|50|warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int **’|
main.c|52|warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int **’|


You may want to see if your compiler has any higher error reporting levels.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 jldavis76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-September 11

Re: Hamming Code program

Posted 28 March 2012 - 05:32 PM

It actually compiled and ran fine. The only problem is that when it shows an error in the hamming code, it shows it off by one bit and I can't figure out how to correct that.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Hamming Code program

Posted 28 March 2012 - 06:36 PM

Please show a sample of your input, what the program is outputting and what you expect the program to output.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 jldavis76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-September 11

Re: Hamming Code program

Posted 28 March 2012 - 06:39 PM

I actually got it to work. I thank you very much for your help Jim. I would've still been looking in the wrong function for a segmentation fault if you had not pointed it out!

Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 jzepeda805  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-October 12

Re: Hamming Code program

Posted 18 October 2012 - 10:22 PM

View Postjldavis76, on 28 March 2012 - 06:39 PM, said:

I actually got it to work. I thank you very much for your help Jim. I would've still been looking in the wrong function for a segmentation fault if you had not pointed it out!

Thanks!



I was wondering where you found the error. I have a similar program i need to write and cant seem to understand your logic. Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1