Question: I keep getting a S egmentation fault: 11 when I try to run my program. The data I use for input is as follows... 1

I keep getting a Segmentation fault: 11 when I try to run my program. The data I use for input is as follows...

1

12

0

2

100011001010010

2

1000110

1

15

1

2

1000110

2

1000111

3

Thank you!

//------------------------------------------------------------------------------------------------------------------------------------------

#include

#include

#include

#include

/** Initializing the global variables */

int MaxLength;

int length;

int parity;

// Initialize the hamming string with a random or NULL memory address

char *HammingString=NULL;

/** Function to enter the values */

void EnterParameters(int *length, int *parity)

{

printf("Enter the maximum length: ");

/** %d reads an integer to be stored in an int. This integer can be signed */

scanf("%d", length);

printf("Enter the parity (0=even, 1=odd): ");

/** %d reads an integer to be stored in an int. This integer can be signed */

scanf("%d", parity);

}

void CheckHamming(char *HammingString, int parity)

{

// Initializing the local variables i, j, k, start, length, ParityNumber

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

printf("Enter the Hamming code: ");

scanf("%s", HammingString);

int ErrorBit = 0; // Initialize the error bit

length = strlen(HammingString); // The strlen computes the length of a string up to, but not including the terminating null character

length--;

if (length > MaxLength)

{

printf(" *** The incorrect Hamming Code is: %s ", HammingString);

printf(" *** Invalid Entry - Exceeds Maximum Code Length of %d ", MaxLength);

return;

}

ParityNumber = ceil(log(length)/log(2)); // The ceil function returns the smallest integer that is greater than or equal to 'x'.

for(i = 0; i < ParityNumber; i++)

{

// pow returns x raised to the power y. In this case, 2 raised to the power i.

start = pow(2, i);

int ParityCheck = parity;

for(j = start; j < length; j=j+(2*start))

{

for(k = j; (k < ((2*j) - 1)) && (k < length); k++)

{

ParityCheck ^= (HammingString[length - k] - '0');

} // End the k for-loop

} // End the j for-loop

ErrorBit = ErrorBit + (ParityCheck * start);

} // End the i for-loop

if(ErrorBit == 0)

{

printf("No error ");

}

else

{

printf(" *** The incorrect Hamming Code is: %s ", HammingString);

printf("There is an error in bit: %d ", ErrorBit);

if(HammingString[length - ErrorBit] == '0')

{

HammingString[length - ErrorBit] = '1';

}

else

{

HammingString[length - ErrorBit] = '0';

}

printf("The corrected Hamming code is: %s ", HammingString);

}

} // End CheckHamming

int main()

{

int parity;

int choice = 0;

printf(" ");

printf("Error detection/correction: ");

printf("---------------------------- ");

printf("1) Enter parameters ");

printf("2) Check Hamming code ");

printf("3) Exit ");

printf(" Enter selection: ");

scanf("%d", &choice);

while (choice != 3)

{

if (choice == 1)

{

EnterParameters(&MaxLength, &parity);

HammingString = (char*) malloc (MaxLength * sizeof(char));

main();

}

else if (choice == 2)

{

CheckHamming(HammingString, parity);

main();

}

else

{

printf("Valid options are 1, 2, or 3. Quitting program. ");

exit(0);

}

}//end while

exit(0);

}//end main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!