5 Replies - 494 Views - Last Post: 03 October 2012 - 05:29 PM Rate Topic: -----

#1 exfoxs28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-August 12

Converting Decimal to binary

Posted 02 October 2012 - 06:19 PM

i've been trying to convert decimal to binary
im having a problem w/ the convertion the output is in reverse position..
when i input 10(decimal) the output must be 1010 but in my program the output is 0101

heres my code
#include <iostream>
using namespace std;

int main()
{
	
	long num, total=0,reverse[100],i;
	cout << "enter a number: ";
	cin >> num;
	while(num>0)
	{
		total = num%2;
		num /= 2;
		cout << total;
	}


system("pause>0");
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Converting Decimal to binary

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: Converting Decimal to binary

Posted 02 October 2012 - 06:54 PM

That's because you're outputting the least significant digit first and the most significant digit last.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Converting Decimal to binary

Posted 02 October 2012 - 07:22 PM

Hi, you need to know the place.

If you are using the array after entering the number you can reverse it. Or you would need to know the size first, which can be found by repeatedly dividing the number by two until the number is zero and counting the size.

If you are using a number to store a binary representation, the place is the old place times 10.

0 -> 1
1 -> 10
2 -> 100
etc.
Was This Post Helpful? 0
  • +
  • -

#4 exfoxs28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-August 12

Re: Converting Decimal to binary

Posted 03 October 2012 - 04:55 PM

@Ctphpnwb thats my problem i dont know why the last digit came first...

@define i want to use array for this but i dont know how.. i already search in the net but theres no tutorial or topic about that..
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Converting Decimal to binary

Posted 03 October 2012 - 05:21 PM

The easiest "fix" would be to push all the values onto a stack, and then to print the binary, just loop through and pop the values off

Alternatively, whenever you find yourself "thinking backwards" that indicates you should maybe use recursion.

example
void printBin(int num) {
   int n = num % 2;
   num /= 2;
   if(num)
      printBin(num); //repeat this funciton while num > 0
   std::cout<<n; 
}


This post has been edited by jjl: 03 October 2012 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Converting Decimal to binary

Posted 03 October 2012 - 05:29 PM

Arrays
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1