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.

Get hands-on with 1200+ tech skills courses.