Hamming Code - Error Detection & Correction - c

This question builds on a few other posts. I understand if this is not relevant to most of the people on the internet but at this point, just like anyone else, I am stuck and cannot find the bug in logic. This question requires checking a specified hamming code for a single-bit error and report/correct the error. Here is the program to do so:
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
/** 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("\n** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", 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 \n");
}
else
{
printf("There is an error in bit: %d\n", ErrorBit);
if(HammingString[length - ErrorBit] == '0')
{
HammingString[length - ErrorBit] = '1';
}
else
{
HammingString[length - ErrorBit] = '0';
}
printf("The corrected Hamming code is: %s \n", HammingString);
}
} // End CheckHamming
int main()
{
int parity;
int choice = 0;
printf("Error detection/correction: \n");
printf("----------------------------\n");
printf("1) Enter parameters \n");
printf("2) Check Hamming code \n");
printf("3) Exit \n");
printf("\nEnter 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. \n");
exit(0);
}
}//end while
exit(0);
}//end main
If the hamming code: 1000110 is entered, the hand-calculated error comes out to an error in but 6 with the corrected code being 1100110. This code displays an error in bit 3 with the corrected code being 1000010. Any help is be greatly appreciated.

I can't quite follow how your code is supposed to work. So here's a simple implementation that computes the syndrome for the code 1000110. The output from the program is 6, i.e. the error is in bit 6.
#include <stdio.h>
int main( void )
{ // 7654321
char input[] = "1000110";
int parity = 0;
for ( int mask = 4; mask; mask >>= 1 )
{
for ( int bit = 1; bit <= 7; bit++ )
if ( bit & mask )
if ( input[7-bit] == '1' )
parity ^= mask;
}
printf( "%d\n", parity );
}

Related

how to extract the even number from user input, and combine them as a new number in C program

test case:
input: 1234
output: 24
input: 2468
output: 2468
input: 6
output: 6
I have this code:
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: \n");
scanf("%d", &num);
int numberLength = floor(log10(abs(num))) + 1;
int inputNumberArray[numberLength];
int evenNumberCount = 0;
int even[10];//new even no. array
int i = 0;
do {
inputNumberArray[i] = num % 10;
num = num / 10;
i++;
} while (num != 0);
i = 0;
while (i < numberLength) {
if (inputNumberArray[i] % 2 == 0) {
evenNumberCount ++;
even[i] = inputNumberArray[i];
}
i++;
}
printf("array count %d\n", evenNumberCount);
i = 0;
for (i = 0; i < 8; i++) {
printf(" %d", even[i]);//print even array
}
i = 0;
int result = 0;
for (i = 0; i < 10; i++) {
if (evenNumberCount == 1) {
if (even[i] != 0) {
result = even[i];
} else {
break;
}
} else {
if (even[i] != 0) {
result = result + even[i] * pow(10, i);
} else
break;
}
}
printf("\nresult is %d", result);
/*
int a = 0;
a = pow(10, 2);
printf("\na is %d", a);
*/
}
when I enter number 1234, the result/outcome is 4, instead of 24.
but when I test the rest of test case, it is fine.
the wrong code I think is this: result = result + even[i] * pow(10, i);
Can you help on this?
Thanks in advance.
why do you have to read as number?
Simplest algorithm would be
Read as text
Validate
loop through and confirm if divisible by 2 and print live
next thing, have you try to debug?
debug would let you know what are doing wrong. Finally the issue is with indexing.
evenNumberCount ++; /// this is technically in the wrong place.
even[i]=inputNumberArray[i]; /// This is incorrect.
As the user Popeye suggested, an easier approach to accomplish this would be to just read in the entire input from the user as a string. With this approach, you can iterate through each letter in the char array and use the isdigit() method to see if the character is a digit or not. You can then easily check if that number is even or not.
Here is a quick source code I wrote up to show this approach in action:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char input[100] = { '\0' };
char outputNum[100] = { '\0' };
// Get input from user
printf("Enter a number: ");
scanf_s("%s", input, sizeof(input));
// Find the prime numbers
int outputNumIndex = 0;
for (int i = 0; i < strlen(input); i++)
{
if (isdigit(input[i]))
{
if (input[i] % 2 == 0)
{
outputNum[outputNumIndex++] = input[i];
}
}
}
if (outputNum[0] == '\0')
{
outputNum[0] = '0';
}
// Print the result
printf("Result is %s", outputNum);
return 0;
}
I figured out the solution, which is easier to understand.
#include <stdio.h>
#include <math.h>
#define INIT_VALUE 999
int extEvenDigits1(int num);
void extEvenDigits2(int num, int *result);
int main()
{
int number, result = INIT_VALUE;
printf("Enter a number: \n");
scanf("%d", &number);
printf("extEvenDigits1(): %d\n", extEvenDigits1(number));
extEvenDigits2(number, &result);
printf("extEvenDigits2(): %d\n", result);
return 0;
}
int extEvenDigits1(int num)
{
int result = -1;
int count = 0;
while (num > 1) {
int digit = num % 10;
if (digit % 2 == 0) {
result = result == -1 ? digit : result + digit * pow(10, count);
count++;
}
num = num / 10;
}
return result;
}
}
You are overcomplicating things, I'm afraid.
You could read the number as a string and easily process every character producing another string to be printed.
If you are required to deal with a numeric type, there is a simpler solution:
#include <stdio.h>
int main(void)
{
// Keep asking for numbers until scanf fails.
for (;;)
{
printf("Enter a number:\n");
// Using a bigger type, we can store numbers with more digits.
long long int number;
// Always check the value returned by scanf.
int ret = scanf("%lld", &number);
if (ret != 1)
break;
long long int result = 0;
// Use a multiple of ten as the "position" of the resulting digit.
long long int power = 1;
// The number is "consumed" while the result is formed.
while (number)
{
// Check the last digit of what remains of the original number
if (number % 2 == 0)
{
// Put that digit in the correct position of the result
result += (number % 10) * power;
// No need to call pow()
power *= 10;
}
// Remove the last digit.
number /= 10;
}
printf("result is %lld\n\n", result);
}
}

How to use a code with arrays and strings?

Here are the instructions I was given:
If the command word is find, read an additional integer and search the data set for that integer.
If the command word is print, print the array
Any other command word is an error.
No command word will be longer than 20 characters.
After reading the n+1 values, there will be one more integer (k) read from the keyboard.
Search the array for the value k. If found, print the location where k was found. (1 = data value, n = last data value).
If k is not found, print not found. This is not an error.
If there are more than one value k in the data, only print the location of the first one.
#include <stdio.h>
int main (void) {
int n;
scanf ("%d", &n);
if (n < 1) {
printf ("Error: one or more values must be provided.\n");
return 1;
}
int x [n];
int a;
a = 0;
while (a < n) {
scanf ("%d", x [a]);
a = a + 1;
}
int k;
scanf ("%d", &k);
int i;
i = 0;
while (i <= n-1) {
if (x[i] == k) {
break;
}
i = i + 1;
}
if (i < n) {
printf ("%d\n", k+1);
} else {
printf ("not found\n");
}
printf ("Error: invalid command\n");
return 0;
}
Suggested Strategy:
After reading the array data, read a string.
If the string is find, read integer k and perform a search.
If the string is print, do not read k, just print the data in the array.
If the string is not find or print, handle the error.
Shai'Tavia, I hope my answer will help you see how you may make your code work. You've got the first part down, but you will need to compare the command string given by the user to then make a decision on what to do next.
#include <stdio.h>
#include <string.h>
#define ARRAYLENGTH 8
void printArray(int *array, int length)
{
for (int i = 0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
void search(int *array, int key)
{
int flag = 0;
for (int i = 0; i < ARRAYLENGTH; i++)
{
if (array[i] == key && flag == 0)
{
printf("found %d at index: %d\n", key, i);
flag = 1;
}
}
if (flag == 0)
printf("not found\n");
}
int main(void)
{
char command[20];
int indx = 0;
int array[] = {1, 4, 6, 8, 43, 61, 34, 2};
int n, flag = 0;
printf("How many times will we run?");
scanf("%d", &n);
if (n < 1)
{
printf("Error: one or more values must be provided.\n");
return 1;
}
do
{
printf("Enter the command word:");
scanf("%s", command);
if (strcmp(command, "find") == 0)
{
scanf("%d", &n);
search(array, n);
}
else if (strcmp(command, "print") == 0)
printArray(array, ARRAYLENGTH);
else
printf("Command not found\n");
} while (--n > 0);
printf("What is your final interger?");
scanf("%d", &n);
search(array, n);
return 0;
}

Coding for multiple modes in C?

My assignment is to find all possible modes for a set of numbers (0 to 100).
We were told to do so using arrays and also to count the frequency that each number occurs.
I've coded for the mode, however, my program does not work is there are multiple modes (example: 1, 2, 7, 7, 9, 10, 7, 2, 2. In this stance, 2 and 7 are both the mode and my program needs to print both of them, but mine doesn't).
I think I might have to make another array set, but I'm not sure? Any advice would be appreciated.
Here is what I have:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
int x, i, c[101], mode;
printf("please enter test scores between 0 and 100\n");
i = 0;
mode = 0;
while (i <= 100) { //setting all values to 0
c[i] = 0;
i = i + 1;
}
scanf("%d", &x); // scanning in the test scores
while ((x >= 0) && (x <= 100)) { // counting how often each score appears
c[x] = c[x] + 1;
if (c[x] >= mode) {
mode = x;
}
scanf("%d", &x);
}
printf("THE MODE(S) ARE %d\n", mode);
i = 0;
while (i <= 100) { //printing all values so long as they've occurred at least once
if (c[i] > 0) {
printf("%d occurs %d times\n", i, c[i]);
}
i = i + 1;
}
}
You have to count the highest frequency of any number and if that frequency equals the frequency of any other number then that number will also be mode.
So the changes that you need to do are:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int x, i, c[101], mode;
printf("please enter test scores between 0 and 100\n");
i = 0;
mode = 0;
while (i <= 100) //setting all values to 0
{
c[i] = 0;
++i;
}
scanf("%d", &x); // scanning in the test scores
while ((x >= 0) && (x <= 100)) // counting how often each score appears
{
c[x] = c[x] + 1;
if (c[x] >= mode)
{mode = c[x];}
scanf("%d", &x);
}
for(i=0;i<=100;i++){//printing all values having highest frequency
if (c[i]==mode)
{
printf("THE MODE(S) ARE %d\n", i);
}
i = 0;
while (i<=100) //printing all values so long as they've occurred at least once
{
if (c[i] > 0)
{
printf("%d occurs %d times\n", i, c[i]);
}
++i;
}
}
Instead of determining the mode in the main entry loop, you should determine the maximum count. Then you can print all values with this count of occurrences in a final loop.
You should also check the return values of scanf().
I would also advise to use an initializer for the array to avoid a loop and to use for loops that more clearly identify the initialization, test and increment of the loop index.
Here is a corrected version of your code:
#include <stdio.h>
int main() {
int x, i, c[101] = { 0 }, max_repeat;
printf("please enter test scores between 0 and 100\n");
max_repeat = 0;
// read the test scores and compute the maximum repeat count
while (scanf("%d", &x) == 1 && x >= 0 && x <= 100) {
c[x] += 1;
if (max_repeat < c[x]) {
max_repeat = c[x];
}
}
printf("The mode(s) are");
for (i = 0; i <= 100; i++) {
if (c[i] == max_repeat) {
printf(" %d", i);
}
}
printf("\n");
return 0;
}

Program loops infinitely if a non-integer is entered, and does not accept plural digit inputs

Assigned task is to ask for # of values, and then at the end output the minimum, maximum, and average values and at this point I've run out of bug fixes
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int ErrorDetection = 1;
char valCounter;
int valnumber;
int Incrementer;
int StoredValue;
int MinimumValue = 100;
int MaximumValue = 0;
float Average;
int AddToStored;
int Sum = 0;
printf("MIN, MAX, and MEAN CALCULATOR\n\n");
while (ErrorDetection != 0)
{
printf("How many values are to be entered?\n");
scanf("%s", &valCounter);
if (valCounter > '0' && valCounter < '9') {
ErrorDetection = 0;
}
else {
ErrorDetection = 1;
printf("INPUT ERROR!\n");
}
valCounter = valCounter - 47;
}
for (Incrementer = 1; Incrementer < valCounter; Incrementer++)
{
ErrorDetection = 1;
while (ErrorDetection != 0) {
printf("Value %d: ", Incrementer);
scanf(" %d", &StoredValue);
if (StoredValue > 0 && StoredValue < 9) {
ErrorDetection = 0;
}
else {
ErrorDetection = 1;
printf("INPUT ERROR!\n");
continue;
}
}
if (StoredValue > MaximumValue) {
MaximumValue = StoredValue;
}
if (StoredValue <= MinimumValue) {
MinimumValue = StoredValue;
}
Sum = Sum + StoredValue;
}
valCounter = valCounter - 1;
Average = (float)Sum / (float)valCounter;
printf(
"Minimum value is %d, maximum value is %d, and average value is %g.\n",
MinimumValue, MaximumValue, Average
);
}
If you input a 2 digit number things begin to breakdown, but at the same time I don't know how to go through with errorchecking if I allow multiple digit answers, as I make use of ASCII conversions to check if an input is a number or not.
You have undefined behavior here.
char valCounter;
scanf("%s", &valCounter);
You have declared valCounter as char type but trying to read string type.
Hence change the scanf to.
scanf("%c", &valCounter);
I would suggest you declare valCounter as int
int valCounter;
scanf("%d", &valCounter);
in that case your if will become.
if ((valCounter > 0) && (valCounter < 9))
and you don't need
valCounter = valCounter - 47; //remove
Also your for loop should start from 0 instead of 1
for(Incrementer = 1 ; Incrementer < valCounter; Incrementer++)
should be
for(Incrementer = 0 ; Incrementer < valCounter; Incrementer++)
Your problem is here.
char valCounter;
scanf("%s", &valCounter);
You're telling scanf to read a string, but you're passing it the address of a character. You should be asking for an integer, and giving it the address of an integer.
int valCounter;
scanf("%d", &valCounter)
There's more information here, including reasons why scanf might not be the best idea:
How to scanf only integer?

C program to find if a number is palindrome or not

I made a C program to check if a number is palindrome or not. I used the following code, but it shows numbers like 12321 as non palindrome. Can you please explain me the mistake in the program below?
#include <stdio.h>
int main()
{
int i, x, n, c, j;
int d=0;
printf ("enter total digits in number: ");
scanf ("%d", &i);
printf ("\nenter number: ");
scanf ("%d", &n);
j=n;
for (x=1; x<=i; x++)
{
c= j%10;
d=c*(10^(i-x))+d;
j=(j-c)/10;
}
if (d==n)
{
printf ("\npalindrome");
}
else
{
printf ("\nnon palindrome");
}
return 0;
}
^ is the xor operator.
In order to raise power, you need to include math.h and call pow
d = (c * pow(10, i - x)) + d;
this algorithm is as simple as human thinking, and it works
#include <stdio.h>
int main() {
int i=0,n,ok=1;
char buff[20];
printf("Enter an integer: ");
scanf("%d", &n); // i am ommiting error checking
n=sprintf(buff,"%d",n); //convert it to string, and getting the len in result
if(n<2) return 0;
i=n/2;
n--;
while(i && ok) {
i--;
//printf("%c == %c %s\n", buff[i],buff[n-i],(buff[i]==buff[n-i])?"true":"false");
ok &= (buff[i]==buff[n-i]);
}
printf("%s is %spalindrome\n",buff, ok?"":"not ");
return 0;
}
// Yet another way to check for palindrome.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, rn, tn;
printf("Enter an integer: ");
scanf("%d", &n);
// reverse the number by repeatedly extracting last digit, add to the
// previously computed partial reverse times 10, and keep dropping
// last digit by dividing by 10
for (rn = 0, tn = n; tn; tn /= 10) rn = rn * 10 + tn % 10;
if (rn == n) printf("%d is palindrome\n", n);
else printf("%d is not palindrome\n", n);
}
A loop like this might do:
int src; // user input
int n; // no of digits
int res = 0;
int tmp; // copy of src
// .... read the input: n and src ....
tmp = src;
for(int i = 0; i < n; i ++)
{
int digit = tmp % 10; // extract the rightmost digit
tmp /= 10; // and remove it from source
res = 10*res + digit; // apend it to the result
}
// ...and test if(res == src)...

Resources