954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary to Decimal

Im doing my project on number conversion. I found the coding below. can anyone help me to explain the coding. why the coding have "count"? why the "count + 1"? what function of "count"?

void Bin2Dec()
{
	long int a[20],i,n,count=0,b[20],c[20],sum=0;
	printf("\n     Enter a binary number to convert to decimal form:");
	scanf("%ld",&n);
	for (i=0;n>=1;i++)
	{
		a[i]=n%10;
		n=n/10;
		count=count + 1;
	}
	for (i=0;i<=count-1;i++)
	{
		b[i]=pow(2,i);
	}
	for (i=0;i<=count-1;i++)
	{
		c[i]=a[i] * b[i];
		sum = sum + c[i];
	}
	printf("     The decimal form for the binary number is %ld\n",sum);

}


All the replies will save my marks..thank you~

BITnur
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

count holds the count of the digits in the binary number.
for eg consider 101.

111 -> count = 3.
b[0] = 1
b[1] = 2
b[2] = 4


c[0] = 1 * 1
c[1] = 0 * 2
c[2] = 1 * 4

Therefore, sum = 6.

PS: What compiler/IDE are you using? Most IDEs provide a debugger, using which, you can add breakpoints, watches and figure out what the program is doing.

myk45
Posting Whiz
319 posts since Sep 2010
Reputation Points: 57
Solved Threads: 40
 

Next time you post code, use the [ CODE ] Tags.

The code you posted is terrible. Not only is it highly inefficient but the variable names are undescriptive and there are no comments anywhere to be found.

Either way, the "count" variable is apparently used to store the number of digits in the given number. It seems to follow this technique .

Feel free to use the DaniWeb search bar to find the literally thousands of other threads talking about this very subject for more information if you are interested.

N1GHTS
Posting Whiz in Training
275 posts since Sep 2010
Reputation Points: 115
Solved Threads: 18
 

thanks to all~~ I'm using compiler Turbo C++~~ :D

BITnur
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: