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.
We'll cover the following...
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
rirepresents the size (the binary representation size ofNumbers[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:
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, 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:
-
int Numbers[],int size: The list of decimal numbers, which we want to convert into binary representations. -
int binaryMap[][MaxCols]: For each decimal number inNumbers[], 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.
- Store the size of the binary representation at
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.