# Numerical computations in C++ : Converting from Decimal to Binary – 2

April 17, 2011 Leave a comment

Earlier we looked at a recursive method for conversion from decimal to binary. We can also achieve the conversion in an iterative manner, however adding to the task the additional overhead of reversing the answer that we obtain.

The underlying method is the one discussed at the beginning of the previous post. At each iteration, we divide a number by 2 and store the corresponding remainder in an array. At the end of the process, we reverse the contents of the array.

Now, we are faced with a small problem. How do we know what the size of our array should be? Fortunately, we can proceed without knowing that, using the resizeable *vector *container class. (A *vector *is like a dynamic array. We need not specify its size at the time of definition.)

In order to reverse the vector, we can simply use the *reverse *algorithm defined in the *algorithm* header. Or if we simply want to output the binary representation (without needing to store it anywhere), we may simply use a *reverse_iterator *to iterate through our vector.

———–

**// function dec_to_bin outputs the binary equivalent of the decimal input n.
**

**#include <iostream>**

**#include <vector> **

**using namespace std;
**

**void dec_to_bin ( int n ) **

**{**

**vector <int> v; // vector of ints to store the binary representation. **

**int temp = n; **

**if ( n < 0) **

**temp = -n; **

**while ( temp != 0) **

**{**

**v.push_back (temp%2); **

**temp / = 2;
**

**}**

**vector<int> reverse_iterator i; **

**if ( n < 0) **

**cout << ” – ” ;
**

**for ( i = v.rend ( ); i != v.rbegin ( ); ++i) **

**cout << *i;
**

**}**

**——-**

If we used a list or a deque instead of a vector, we wouldn’t be faced with the task of having to reverse the values computed by us. We can do this by storing successive numbers that we generate at the front of the data structure instead of at the end.

We may do that using the *push_front *member function for lists and deques. (Note that we cannot use *push_front *for a vector. We can only insert and delete elements (efficiently i.e. in constant time) at the *back *of a vector.)

——

The above program using a deque along with *push_front *would look as follows:

——

**#include <iostream> **

**#include <deque> **

**#include <algorithm> **

**using namespace std;
**

**void dec_to_bin ( int n ) **

**{ **

**deque <int> v; **

**int temp = n;
**

**if ( n < 0) **

**temp = -n; **

**while (temp != 0) **

**{**

**v.push_front ( temp%2); **

**temp /=2;
**

**}**

**if ( n < 0) **

**cout << “-“; **

**copy (v.begin( ), v.end ( ), ostream_iterator<int> (cout,””);
**

**} **

**——-**

Instead of using an iterator to iterate through the deque, we can use the *copy *algorithm defined in the header *algorithm *to directly output the contents of the deque. (Note that the statement #include <algorithm> though written as part of the above program is not necessary, since *algorithm.h *automatically gets included when we write *#include <deque>. ) *

——-