14 Replies - 1691 Views - Last Post: 12 August 2009 - 07:21 PM Rate Topic: -----

#1 shafqatbaboo  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 04-August 09

Decimal to Binary Converter

Post icon  Posted 12 August 2009 - 04:59 AM

I want to create a program that convert decimal number to binery
here is my code that not work:

#include <iostream.h>
#include <iomanip.h>

using namespace std;

void Deci2Bin();


int main()
{
	int number1;
	int number2;

	cout<<"======================================="<<endl;
	cout<<"=	  Decimal to Binary Converter	="<<endl;
	cout<<"=======================================\n"<<endl;

	cout<<"Please enter first decimal number : ";
	cin>>number1;
	cout<<"Please enter second decimal number: ";
	cin>>number2;



	cout<<"======================================="<<endl;
	cout<<"First Integer : "<<number1<<"="<<Deci2Bin<<endl;
	cout<<"Second Integer: "<<number2<<"="<<endl;
	return 0;
}
void Deci2Bin()

  int num;
  int bin[25];
  int i;
  for(i=0;num>0;i++)


			{
			bin[i]=num%2;
			num/=2;
			}
			for(--i;i>=0;i--)
			cout<<bin[i];





Please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Decimal to Binary Converter

#2 bimbolena  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-August 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:08 AM

Hi!

Deci2Bin must have a parameter for the number and be called as a function. I've changed that part below, hope it helps.

/Lena

#include <iostream.h>
#include <iomanip.h>

using namespace std;

void Deci2Bin(int num);


int main()
{
	int number1;
	int number2;

	cout<<"======================================="<<endl;
	cout<<"=	  Decimal to Binary Converter	="<<endl;
	cout<<"=======================================\n"<<endl;

	cout<<"Please enter first decimal number : ";
	cin>>number1;
	cout<<"Please enter second decimal number: ";
	cin>>number2;



	cout<<"======================================="<<endl;
	cout<<"First Integer : "<<number1<<"="; 
	Deci2Bin(number2); 
	cout<<endl;
	cout<<"Second Integer: "<<number2<<"=";
	Deci2Bin(number2);
	cout<<endl;
	return 0;
}
void Deci2Bin(int num)
{ 
  int bin[25];
  int i;
  for(i=0;num>0;i++)
			{
			bin[i]=num%2;
			num/=2;
			}
  for(--i;i>=0;i--)
			cout<<bin[i];
}



Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:34 AM

this should shed some light...

hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 shafqatbaboo  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 04-August 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:40 AM

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:42 AM

which code?
Was This Post Helpful? 0
  • +
  • -

#6 kewlkreator  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 38
  • View blog
  • Posts: 1,065
  • Joined: 25-March 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:45 AM

DreamInCode has a request that you post your code like :code:. This will keep your post organized and easy to tell apart. Please edit this.
Was This Post Helpful? 0
  • +
  • -

#7 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Decimal to Binary Converter

Posted 12 August 2009 - 06:05 AM

View Postshafqatbaboo, on 12 Aug, 2009 - 11:40 AM, said:

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

in main()
	cout<<"======================================="<<endl;
	cout<<"First Integer : "<<number1<<"=";
	Deci2Bin(number2);
	cout<<endl;
	cout<<"Second Integer: "<<number2<<"=";
	Deci2Bin(number2);
	cout<<endl;
	return 0;


both times you call Deci2Bin() with number2 (hence you get 1000 printed both times) - the first time should be with number1, i.e.
	cout<<"======================================="<<endl;
	cout<<"First Integer : "<<number1<<"=";
	Deci2Bin(number1);
	cout<<endl;
	cout<<"Second Integer: "<<number2<<"=";
	Deci2Bin(number2);
	cout<<endl;
	return 0;
}


in Deci2Bin() a faster alternative to using the % operator
			bin[i]=num%2;


is &, i.e.
			bin[i]=num&1;


This post has been edited by horace: 12 August 2009 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#8 tobix10  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 06-April 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:22 AM

 for(--i;i>=0;i--)
			cout<<bin[i]; 


look at this for statement
Was This Post Helpful? 0
  • +
  • -

#9 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:39 AM

edited

This post has been edited by computerfox: 12 August 2009 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:58 AM

View Posttobix10, on 12 Aug, 2009 - 02:22 PM, said:

 for(--i;i>=0;i--)
			cout<<bin[i]; 


look at this for statement

what is wrong with it? it prints out the binary value most significant bit first
Was This Post Helpful? 0
  • +
  • -

#11 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Decimal to Binary Converter

Posted 12 August 2009 - 09:42 AM

you know if you're writing a code for others to read, your code should make sense .. unless if you're making it complex for others to reas,
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:16 AM

Code runs fine. You can test it with a loop, if you like.

#include <iostream>

using namespace std;

// pass a number
void Deci2Bin(int num) {
	int bin[25];
	int i;
	
	// load up the array
	for(i=0;num>0;i++) {
		bin[i]=num%2;
		num/=2;
	}
	
	// read it back out backwards
	// note, need to decrement i first
	for(--i;i>=0;i--) {
		cout << bin[i];
	}
}


int main() {
	for(int i=0; i<20; i++) {
		cout << i << "\t";
		Deci2Bin(i);
		cout << endl;
	}
	return 0;
}



A slightly cleaner way would be to use a string. That way you could also return it.

Which got me to thinking, if I really wanted to use an array for this, I'd go with a char array.

void Deci2Bin(int num) {
	const int MaxSize = 65; // you know, an int could be that big + 0
	char s[MaxSize];
	int pos = MaxSize; // init pos
	
	// the trick with pos, it must always point to current
	s[--pos] = 0; // terminate our string
	
	// load up the array
	while(num>0) {
		s[--pos] = (num%2) ? '1':'0';
		num/=2;
	}
	
	char *p = &s[pos]; // fake out cout
	cout << p;
}




You can actually do a recursive solution to this in three lines. I'll leave that as an exercise for the reader.

This post has been edited by baavgai: 12 August 2009 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

#13 bimbolena  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-August 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:42 AM

Ooops my fault. Typed number2 twice when I made the answer for him. Or can I claim it wa intentional to check and see if he read and understand the code himself?

Lena

View Posthorace, on 12 Aug, 2009 - 05:05 AM, said:

View Postshafqatbaboo, on 12 Aug, 2009 - 11:40 AM, said:

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

in main()
	cout<<"======================================="<<endl;
	cout<<"First Integer : "<<number1<<"=";
	Deci2Bin(number2);
	cout<<endl;
	cout<<"Second Integer: "<<number2<<"=";
	Deci2Bin(number2);
	cout<<endl;
	return 0;


both times you call Deci2Bin() with number2 (hence you get 1000 printed both times) - the first time should be with number1, i.e.

Was This Post Helpful? 0
  • +
  • -

#14 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:52 AM

that's so mean lena lol

any further questions?
Was This Post Helpful? 0
  • +
  • -

#15 shafqatbaboo  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 04-August 09

Re: Decimal to Binary Converter

Posted 12 August 2009 - 07:21 PM

Thanx very much.
Sir I also want to test AND,OR and Exclusive OR with these two number.
Help me for testing the result. Thankfull to all of you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1