Search⌘ K
AI Features

Problem Solving: Number Conversion

Explore how to solve number conversion problems by converting decimal numbers into binary representations using multi-dimensional arrays. Understand how to calculate binary sizes, store the binary data efficiently, and display the result clearly. This lesson helps you build functions for binary conversion, work with 2D arrays, and enhance your problem-solving skills in programming.

In this lesson, we have a challenge for you.

Binary conversion

Take an integer N, followed by a stream of N numbers. The stream should be loaded in an array. Then your program should calculate the binary representation of each number in a separate 2D array (where every binary representation should be stored in a single dimensional array along with its representation size on the first index of the array). In the end, the program should display each number’s binary representation.

Here’s the sample file:

Numbers.txt
-----------
5
8 15 3 28 47

Output:
Decimal   size  Binary    
8         4     1 0 0 0 
15        4     1 1 1 1 
3         2     1 1 
28        5     1 1 1 0 0 
47        6     1 0 1 1 1 1 

Here’s the pictorial representation of the 2-D structures we would like to build:

  • An array, Numbers[], loaded with all decimal numbers from the file.
  • A 2-D map, where each row ri represents the size (the binary representation size of Numbers[ri]) followed by its binary representation.

Here’s the sample output of the program:

Decimal   size  Binary    
8         4     1 0 0 0 
15        4     1 1 1 1 
3         2     1 1 
28        5     1 1 1 0 0 
47        6     1 0 1 1 1 1 

Playground for your implementation

Write your code below:

C++
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define MaxRows 100
#define MaxCols 100
#define Capacity 100
int binarySize(int aNumber)
{
// Write your code here
}
void convertToBinary(int binaryMap[][MaxCols], int Numbers[], int size)
{
// Write your code here
}
void displayBinary(int binaryMap[][MaxCols], int Numbers[], int size)
{
// Write your code here
}
int main()
{
ifstream Rdr("Numbers.txt");
int size;
Rdr >> size;
int binaryMap[MaxRows][MaxCols];
int Numbers[Capacity];
for (int i = 0; i < size; i++)
{
Rdr >> Numbers[i];
}
convertToBinary(binaryMap, Numbers, size);
displayBinary(binaryMap, Numbers, size);
return 0;
}

Steps to follow for decimal to binary conversion

Let’s write our program step by step. We have turned it into tasks.

Task 1: Finding decimal to binary representation size

We should make a function:

int binarySize(int aNumber);

This function receives a decimal number and returns the size of its binary representation. For example, 2323 has a binary representation with 5 digits.

We will be needing this function while calculating the binary representation of each number.

Hint: Look at the animation below carefully. All you need to do is maintain a counter and keep dividing the given number by 2 until the number becomes zero. The number of times the repeated division happens is the exact size of the binary representation.

Instruction: Add the code for this task 1 in your playground.

Task 2: Converting each decimal number to binary and storing in the map

Now let’s make the following function:

void convertToBinary( int Numbers[], int size, int binaryMap[][MaxCols])

It has the following parameters:

  1. int Numbers[], int size: The list of decimal numbers, which we want to convert into binary representations.

  2. int binaryMap[][MaxCols]: For each decimal number in Numbers[], this function stores the binary representation in this map (where the zeroth index in each row represents the size of each binary representation followed by the actual representation).

Hint: Look at the animation below carefully. You need to do the following:

  • For each number Number[i],
    • Store the size of the binary representation at binaryMap[i][0], then keep dividing the number by 2 and along with that store the remainder in the reverse order in reverse order.

Look at the following animation for clarity:

Task 3: Displaying the number and its corresponding binary representation

You have to write the following function now:

void displayBin(int binaryMap[][MaxCols], int Numbers[], int size);

This function should display the following output:

Decimal   	Binary
 8  :   	1 0 0 0 
 15 :   	1 1 1 1 
 3  :    	1 1 
 28 :   	1 1 1 0 0 
 47 :   	1 0 1 1 1 1 

Here's the complete animation of how the conversion will happen.

Complete implementation

C++
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define MaxRows 100
#define MaxCols 100
#define Capacity 100
int binarySize(int aNumber)
{
int counter = 0;
if (aNumber == 0)
counter++;
while(aNumber > 0)
{
aNumber = aNumber / 2;
counter++;
}
return counter;
}
void convertToBinary(int binaryMap[][MaxCols], int Numbers[], int size)
{
for (int i = 0; i < size; i++)
{
int value = Numbers[i];
int counter = binarySize(Numbers[i]);
binaryMap[i][0] = counter;
for (int j = counter; j > 0; j--)
{
binaryMap[i][j] = value % 2;
value /= 2;
}
}
}
void displayBinary(int binaryMap[][MaxCols], int Numbers[], int size)
{
int binSize = 0;
cout <<left<< setw(10)<< "Decimal" << setw(6) << "size" << setw(10) << "Binary"<<endl;
for (int i = 0; i < size; i++)
{
binSize = binaryMap[i][0];
cout << left << setw(10)<<Numbers[i] << setw(6) << binSize ;
for (int j = 1; j <= binSize; j++)
cout<<binaryMap[i][j] << " ";
cout << endl;
}
}
int main()
{
ifstream Rdr("Numbers.txt");
int size;
Rdr >> size;
int binaryMap[MaxRows][MaxCols];
int Numbers[Capacity];
for (int i = 0; i < size; i++)
{
Rdr >> Numbers[i];
}
convertToBinary(binaryMap, Numbers, size);
displayBinary(binaryMap, Numbers, size);
return 0;
}