Solved

C array of characters

Posted on 2011-03-19
11
551 Views
Last Modified: 2012-05-11
I have attached the code below, and don't understand what this line of code means:

error_bit = error_bit + (current_parity ^ (hamming [hamming_length-i ] - '0' )) * i;

Specifically, I don't understand what it means to subtract the '0' in the check_hamming_code function.

By the way, the user specifies:
hamming_length = 7
parity_type = 0
hamming = 1000110

 
void params() {
	printf("Enter the length of the Hamming code: ");
	scanf("%d", &hamming_length);
	printf("Enter the parity (o=even, 1=odd): ");
	scanf("%d", &parity_type);

	hamming = (char *) malloc (hamming_length * sizeof(char));

	return;
}

/* Option #2 */

void check_hamming_code() {
	/* declare local var's */
	
	int i,j,k;

	/* prompt for hamming code string */
	printf("Enter the Hamming code: ");
	scanf("%s",hamming);

	for(i=1; i < hamming_length; i*=2) {
		/* init. something in i */
		int current_parity = parity_type;

		for(j=i; j <= hamming_length; j+=2*i) {
			/* init. something in j */

			for(k=j; (k <= hamming_length) && (k < i+j); k++) {
				
				if(k != i) 
					current_parity = current_parity ^ hamming[hamming_length-k];
			} /* end of k loop */
		} /* end of j loop */
		error_bit = error_bit + (current_parity ^ (hamming[hamming_length-i]-'0'))*i;
	} /* end i loop */

	/* correct error bit */

	printf("%d",error_bit);

	return;
}

Open in new window

0
Comment
Question by:InfoTechEE
11 Comments
 
LVL 5

Assisted Solution

by:daohailam
daohailam earned 100 total points
ID: 35172772
"hammning" is a string of digit characters ('0' - '9'). By subtracting hamming[ i ] by '0', the code converts a character i-th in hamming string to a number.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 35172777
>>Specifically, I don't understand what it means to subtract the '0' in the
>>check_hamming_code function.

The user input is given is ASCII (http://en.wikipedia.org/wiki/ASCII) and placed in a char array. Subtracting the ASCII value of '0' as done in the line in question converts the character at 'hamming[hamming_length-i]' to its numerical equivalent, e.g. if the character entered was '3' (ASCII 51), subtracting '0' (ASCII 48) results in the value '3'.
0
 

Author Comment

by:InfoTechEE
ID: 35172782
How so? Does '0' hold a special value or something?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:InfoTechEE
ID: 35172784
jkr: sorry you answered my question before I asked it.
0
 

Assisted Solution

by:edsont
edsont earned 100 total points
ID: 35172789
The scanf("%s", hamming) function execution will fill the hamming array with a string value, for instance, "1000110", which will be stored as 0x31 0x30 0x30 0x30 0x31 0x31 0x30 in the memory.

Each 0x31's and 0x30's should represent a bit, but they are actually ASCII characters. In order to execute a bit operation, it is needed to convert each of them to bits, and it is done by subtracting '0' in the function. The '0' character value is 0x30.
0
 

Author Comment

by:InfoTechEE
ID: 35172792
Just a follow up question, the reason why i'm going through this code is because the results are suppose to look like this:

Enter the Hamming Code: 1000110
There is an error in bit: 6 (error_bit)
The corrected Hamming code is: 1100110

My results show error_bit = 342

Can anyone see why?
0
 
LVL 86

Expert Comment

by:jkr
ID: 35172820
Try to initialize 'error_bit' with '0' at the beginning of 'check_hamming_code()'. It could have any arbitrary value when that function is called and line 36 in your above snipped just adds to this variable.
0
 

Author Comment

by:InfoTechEE
ID: 35172826
It's a Global Variable initialized to 0. It is an INT.

Here is the part right above the code I attached at the begginning of this post:

#include <stdio.h>
#include <stdlib.h>
/* declare global var's */

int hamming_length=0, parity_type=-1, error_bit=0;

/* declare & initialize hamming string */

char *hamming = NULL;

/* Option #1 */
0
 
LVL 86

Expert Comment

by:jkr
ID: 35172861
Could you post the complete code?
0
 

Author Comment

by:InfoTechEE
ID: 35172874
Found the problem...same as my original question.


Line 36 had subtracted the '0', but line 33 did not.

Thanks everyone for your help. I might be back here soon, but for now closing this.
0
 

Author Comment

by:InfoTechEE
ID: 35173184
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 209
Unresolved External Symbols 3 80
Arduino EDI - Programming language 3 91
What is atomic operation? 6 58
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question