2

I am currently stuck on an assignment and have looked nearly everywhere for even a hint at what I am trying to do.

The assignment is simple, we are to be given a binary number in the form of a vector (e.g. [1,1,1,1] and we are to compute the decimal form of this number and put in back into the same vector form (e.g. [1,5] for the answer to the previous example).

While at first I thought this would have an easy solution, I soon found that we are to use this method to calculate extremely large numbers such as 300 1's in binary.

Now after I realized my mistake of trying to straight up calculate it, I soon found the "divide-and-conquer" method idea but I did not find a single place that gave a precise example of how to use it in this context.

Since this is an assignment, I would rather an answer be proposed that actually explains the concept and provides examples rather than a straight up block of code.

Thank you in advance,

Matthew

Mbiz
  • 21
  • 1
  • 3
  • How exactly does `[1,1,1,1]` correspond to `[1,5]`? – Beta Sep 18 '13 at 18:05
  • OP probably means that binary 1111 equals to decimal 15 – taocp Sep 18 '13 at 18:07
  • Yes, I apologize, [1,5] would be equal to 15, I had that originally, I must have deleted it – Mbiz Sep 18 '13 at 18:11
  • http://www.wikihow.com/Convert-from-Binary-to-Decimal dude... this can be done extraordinarily fast. – progrenhard Sep 18 '13 at 18:18
  • I understand that this seems incredibly easy, please understand that we are expected to store a decimal equivalent to 300 1s, this number will be extremely large, it cannot be held in an int data type. This is the issue that is at hand, easy solutions had me at first, now I am not so sure. – Mbiz Sep 18 '13 at 18:24

2 Answers2

0

Write a base 10 math engine.

It should include addition by another base 10 number, and multiplication by int. (well doubling is enough)

Iterate over the binary digits, keeping track of a base 10 number that corresponds to the digit.

Conditionally accumulate.

The only hard part is the base 10 math system, everything else takes 3 to 8 lines of code.

Sadly, there is very limited easy ways to do thus more efficiently, as any digit of a binary number can influence any digit of the equivalent base 10 number. There may be fancy ways, but for 300 digits you should not bother.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Excellent, I am not sure I understand you though, do you have an example or a snippet that you could show. My brain is currently a little dead from being sick for the past few days. – Mbiz Sep 18 '13 at 18:33
  • Multiplication isn't necessary for this task - once you have addition, `n*2` is the same as `n+n`. Multi-digit addition is really simple, especially if you don't need to consider negative numbers. – Mark Ransom Sep 18 '13 at 19:01
  • @MarkRansom `n+n` looks like doubling to me. :) Doubling is enough (ie, `n=n+n`), but as it happens "multiplication by an `int`" is really, really easy to write. – Yakk - Adam Nevraumont Sep 18 '13 at 19:12
  • 1
    @Mbiz What have you tried? Can you write a function called `add` takes `[1,5]` and `[2,6]` and returns `[4,1]`? If not, I doubt I can help you. If yes, why haven't you already done it? – Yakk - Adam Nevraumont Sep 18 '13 at 19:13
  • I can do math and convert numbers that are under that max for int, the issue I am having as seen below in the answer that was given is the size of the number. – Mbiz Sep 18 '13 at 19:17
  • @mbiz do not convert to to int. Just do addition, longhand, on the digits. – Yakk - Adam Nevraumont Sep 18 '13 at 19:31
  • After having spoken with a colleague of mine, longhand addition with vectors seems like the answer, would it be possible to see an example of this being done? – Mbiz Sep 18 '13 at 20:11
  • 1
    @Mbiz If you want examples of long addition, look at a grade 2 math textbook. – Yakk - Adam Nevraumont Sep 18 '13 at 20:19
  • I know long addition, there is no need to be an ass. Seeing it done between two vectors with carry over is where I was lost. – Mbiz Sep 18 '13 at 21:02
  • @Mbiz, Yakk is right. The mechanics of long addition are the same no matter if you're doing them on paper or with a vector. The only tricky bit is when a new digit needs to be added to the output, but that's easy if you store the digits in reverse order - a simple `push_back` will suffice. In fact everything gets easier if you do it in reverse. – Mark Ransom Sep 19 '13 at 22:49
-1

Following is the solution to your problem:

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>

using namespace std;

string Reverse(string input){
    string copy(input);
    reverse(copy.begin(), copy.end());
    return copy;
}


string Accumulate(vector<double> vNumbers){
    // Do some processign here to add these doubles
    return string("");
}

string Convert(string input){
    input = Reverse(input);
    int power =0;
    vector<double> vNumbers;

    for(string::iterator it=input.begin();it!=input.end();it++){
         if(*it=='1')
             vNumbers.push_back(pow(2, power));
         power++;
    }


    return Accumulate(vNumbers);
}


void main(){
 string s = "0110 0010 0010 1000 0000 1011 0110 1111 0010 0000 1101 1101 0101 0010 0011 0111 0001 0001 0010 0100 1110 0110 0010 0010 1000 0000 1011 0110 1111 0010 0000 1101 1101 0101 0010 0011 0111 0001 0001";

 cout << "input:" << s.c_str() << endl;
 cout << "Output:" << Convert(s).c_str() << endl;
}

The names are self explanatory. here are the steps performed:

  1. Take the string as input
  2. Reverse the string using the std library function coz the processing starts from the right side i.e. unit digit in the number.
  3. Initialize the power and counter to 0
  4. the formula for converting 1110 to decimal: (1)*2*2*2 + (1)*2*2 + (1)*2 + (0)*1 = 14
  5. iterate the characters of string one by one; if the character is 1 calculate the 2^power and add to counter. if it is 0 then ignore
  6. increment the power by 1
  7. convert the counter, which is the answer, to string
  8. Write the Accumulate() yourself.
Simple Fellow
  • 4,315
  • 2
  • 31
  • 44