Please review to add binary numbers!! | SoloLearn: Learn to code for FREE!

+3

Please review to add binary numbers!!

https://code.sololearn.com/c6ppd4wJ66Jm/?ref=app

11/24/2020 4:52:35 PM

Ankit Sharma

11 Answers

New Answer

+4

Do you mean something along the lines of this... https://code.sololearn.com/c7OOifnYi6qs/#cpp Here I have declared the function prototypes at the start of the program. This is known as a 'top down' approach, since the main routine appears at the top of the program. I find this preferable since you can see what inputs are required by the main routine without having to scroll down to the end of the source code. It also means that it does not matter in which order the routines are written. This is particularly important with more complex source code as it can be difficult to maintain all of the functions in the correct order without function prototypes. Since the bin2int routine is a simple one liner I would probably declare this as an inline function if I was going to be calling it a lot in the program. Inline functions get expanded in place by the compiler. This reduces the overhead of a function call and can speed up a program. It is not necessary in this case though.

+4

Your code is incomplete, it was truncated because it exceeds character limits. You should save your code in SoloLearn and share the saved code link instead of raw text code like this. Follow this guide to sharing links 👇 https://www.sololearn.com/post/75089/?ref=app

+3

Why are you making your life so complicated? #include <iostream> // std::cout #include <string> // std::string, std::stoi #include <bitset> // std::bitset using namespace std; int main(void){ int a, b; string buffer; const int NUMBITS = sizeof(int)*8; bitset<NUMBITS> bits; cout << "Enter first binary number: " << endl; cin >> buffer; a = stoi(buffer, nullptr, 2); cout << "Enter second binary number: " << endl; cin >> buffer; b = stoi(buffer, nullptr, 2); bits = bitset<NUMBITS>(a+b); cout << bits; return 0; } That's all there is to it. If you want a shorter binary number just change NUMBITS to 8 or 16 if that's all the precision you want.

+2

Ankit Sharma Thanks for uploading the code 👍 Can you describe me the program purpose? possibly some examples of inputs and expected output. I need that to test the code.

+2

It's a programme to add binary numbers Input two binary numbers And get their sum as output E.g. Input 1 1 Output 10

+2

Ok I'll take a look at it I'll be back a while later ...

+2

Ankit Sharma Martin Taylor had shown you the C++ way of handling binary numbers using std::bitset. I was thinking you were doing what you did for a school tssk hence the self-written code. Where the use of std::bitset was allowed for the task, then you may get yourself acquainted to it here 👇 http://www.cplusplus.com/reference/bitset/bitset/

+2

I have to do it via functions and function should also call other function in itself like here ans function calls reverse. I have to do it without arrays.

+2

The array is used to simplify the code, without the array we'd have to write the bit accumulation part twice, once for <n1> and once more for <n2>. Here I separate the bit accumulation part into another function named `bin_to_dec`, the function is called by `addBinary` function passing <n1> and <n2>. int bin_to_dec(int n) { int result = 0, bit = 1; while(n) { int digit = n % 10; if(digit == 1) result += bit; bit <<= 1; n /= 10; } return result; } int addBinary(int n1, int n2) { return bin_to_dec(n1) + bin_to_dec(n2); } Considering the limit of `int` type, it is also worth noting that input will be limited to 10 digits only.

+1

https://code.sololearn.com/c6ppd4wJ66Jm/?ref=app

+1

Ankit Sharma I haven't figured out your problem yet, but I came out with a shorter version of the function. I think I may need more time to fully check your function for the problem, maybe someone else here can help sooner though. If you want, you can see my implementation, it goes like this int addBinary(int n1, int n2) { int array[2] = {n1, n2}; int result = 0; for(int i = 0, bit, digit, temp; i < 2; i++) { temp = 0; bit = 1; while(array[i]) { digit = array[i] % 10; if(digit == 1) { temp += bit; } bit <<= 1; array[i] /= 10; } result += temp; } return result; }