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~

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.