C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2006, 05:53 PM   #1
Registered User
 
Join Date: Feb 2005
Posts: 21
Decimal to Binary Conversion program

Hello, I wrote this program for my Intro to Digital Systems class to convert a base 10 number into a Binary base 2 number string. It works perfectly except for some numbers it slams a number in front of it. On linux this number is -1073742772. Such a number is 60.
Code:
#include <stdio.h>


int main()
{
	int input=0;
	int count=0;
	int binary_backwards[32];
	int binary_string[32];
	int i=0;
	int j=0;
	printf("Enter a base 10 positive number to convert to binary:");
	scanf("%i", &input);

	while(input >0 && count<32)
	{	
		binary_backwards[count] = input %2;
		input /=2;
		count++;
	}

	while(count >-1)
	{
		binary_string[i]=binary_backwards[count];
		count--;
		i++;
	}
	
	while(j<i)
	{
		printf("%i", binary_string[j]);
		j++;
	}
	return 0;
}
Anyone have any ideas why this is happening?

Last edited by acidbeat311; 01-12-2006 at 05:56 PM.
acidbeat311 is offline   Reply With Quote
Old 01-12-2006, 06:06 PM   #2
aoeuhtns
 
Join Date: Jul 2005
Posts: 581
You incremented count after inserting your last binary digit into binary_backwards.
Rashakil Fol is offline   Reply With Quote
Old 01-12-2006, 06:19 PM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
You should also consider not using fixed numbers (32), and instead consider using provided limits instead:
Code:
#include<limits.h>
...
for( x = 0; x < sizeof( int ) * CHAR_BIT; x++ )
    ...
This will ensure that it works correctly for say, old crappy DOS compilers that most people around here seem to use.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 01-12-2006, 08:38 PM   #4
Registered User
 
Join Date: Dec 2005
Location: Australia - Melbourne
Posts: 63
here is what i would do to fix it

replace the code below which is in your program
Code:
while(count >-1) {
    binary_string[i]=binary_backwards[count];
    count--;
    i++;
}

while(j<i) {
    printf("%i", binary_string[j]);
    j++;
}
with this

Code:
while ((--count) > -1) 
    printf("%d", binary_backwards[count]) ;
hence you can also get rid of
Code:
int binary_string[32];
int i=0;
int j=0;
i'm new to c aswell hope i've been a help.
peterchen is offline   Reply With Quote
Old 01-12-2006, 09:48 PM   #5
Logic Programmer
 
logicwonder's Avatar
 
Join Date: Nov 2005
Location: Kerala, India
Posts: 52
You can do this in more simpler!
__________________
L GIK wins!!!
Salutes from logicwonder
Enjoy programming
logicwonder is offline   Reply With Quote
Old 01-12-2006, 10:26 PM   #6
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,005
A different tack:
Code:
#include <stdio.h>

void dectobin(int value)
{
   if ( value )
   {
      dectobin(value / 2);
      printf("%d", value % 2);
   }
}

int main(void)
{
   dectobin(12345);
   putchar('\n');
   dectobin(60);
   putchar('\n');
   return 0;
}

/* my output
11000000111001
111100
*/
__________________
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Dave_Sinkula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Client-server system with input from separate program robot-ic Networking/Device Communication 3 01-16-2009 03:30 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
decimal to binary kurz7 C Programming 8 07-10-2003 12:03 AM
decimal to binary conversion noob2c C Programming 4 05-29-2003 08:07 PM
Decimal Points and Binary Points Shadow12345 A Brief History of Cprogramming.com 9 11-07-2002 01:06 AM


All times are GMT -6. The time now is 08:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22