2

everyone ! Now I am stuck here with a problem...

Problem:

Given a binary number represented as an array, write a function that takes the array and its size as a parameter, and returns the integer value. You may assume that there are at least 1 and no more than 30 numbers in the array and that all the values are either 0 or 1. The array is ordered with most significant binary digit at the start (index 0) and the least significant digit at the end.

Signature: int binary_to_number(int binary_digits[], int number_of_digits)

The function I have wrote is at the bottom. It works fine to return the int value for number_of_digits <= 10.

As you can see, that the question says "You may assume that there are at least 1 and no more than 30 numbers in the array"

My question is, how can I fix my function to return proper int value even if there is more than 10 numbers (perhaps 30 numbers)?

OR, Should I be approaching the problem different way? and if so, what should I do?

#include<iostream>
#include<string>

int binary_to_number(int binary_digits[], int number_of_digits){
    std::string bin_str;

    for (int i=0; i<number_of_digits; i++) {
         if (binary_digits[i] == 0) {
             bin_str = "0" + bin_str;
         } else if (binary_digits[i] == 1) {
             bin_str = "1" + bin_str;
           } 
    }
    int bin_int = std::stoi (bin_str);
return bin_int;
}
Community
  • 1
  • 1
AyeYo
  • 71
  • 1
  • 6
  • 1
    "It works fine to return the int value for number_of_digits <= 10" How do you know, that it works fine for less, and for more not? Which test cases did you use? – RoQuOTriX May 13 '20 at 13:19
  • 7
    Your function is odd. It takes an array like `[1001]`, then converts it to a string `"1001"`, and converts that to an int, which is `1001`. The answer should actually be `9` it seems. – cigien May 13 '20 at 13:19
  • 2
    Maybe `return static_cast(std::bitset<30>(bin_str).to_ulong());`? Of course this assumes `bin_str` is created correctly. – PaulMcKenzie May 13 '20 at 13:20
  • 1
    you misunderstood the assignment two ways. First it says that the most significant bit comes first, but you put it last, then you should convert to decimal. Your function returns `1` for input `{1,0}` but it should be `2` – 463035818_is_not_an_ai May 13 '20 at 13:21
  • 1
    the thing is that once you do what the assignments asks for, the problem with number of digits will probably be gone. Right now you are limited by digits of an `int` but you don't really need to hit that limit – 463035818_is_not_an_ai May 13 '20 at 13:24
  • 1
    Oh no, I think I completely misunderstood the problem itself... thank you everybody for the correction ! I think I may know what to do now ! – AyeYo May 13 '20 at 13:31
  • 1
    The point of this exercise is that you learn how a binary number is built up from powers of two and a decimal number of powers of ten. The point is not the conversion itself. (In most learning exercises, the method and how to reach it is the point, not the result.) – molbdnilo May 13 '20 at 13:51

3 Answers3

0

first of all u are using 0 and 1's in your string so assume your array is of 30 length then string will also be of 30 length but max size of long long even is 18 digits you will get overflows

int bin_int = std::stoi (bin_str);

above is completely wrong...

You must convert the binary to integer the old way in powers of 2 then if u want to convert it to int no problem but i would suggest you to do like this instead else your approach will be messy

num += pow(2,i); // watch out for overflows if u experience such; then 
                 // better use bit manipulation for 2's power eg.(1<<i)

here no problem just the old way.....But it's starting from LSB . From MSB you have to start from last of array as per your question

Ryednap
  • 324
  • 1
  • 9
0

You may use this algorithm to do so:

int conversion(int array[], int len) {
    int output = 0;
    int power = 1;

    for (int i = 0; i < len; i++)
    {
        output += array[(len - 1) - i] * power;
        // output goes 1*2^0 + 0*2^1 + 0*2^2 + ...
        power *= 2;
    }

    return output;
}

A sample statement could be considered as:

int arr[16] = {1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1};
std::cout << conversion(arr, 16);

Then it should out:

39321

A beautiful representation for my code could be found here.

Hope it helps.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

You are performing more operations than necessary.

int result = 0;
int index = number_of_digits - 1;
for (int i = 0U; i < number_of_digits; ++i)
{
  result = (result << 1) | binary_digits[index];
  --index;
}

The result is initialized to zero.
For each bit in the array:
Left shift the result by one bit to make room for the new bit.
Arithmetic OR the new bit.

Try out the algorithm by hand.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154