Adding binary numbers in C++ - c++

How would I add two binary numbers in C++? What is the correct logic?
Here is my effort, but it doesn't seem to be correct:
#include <iostream>
using namespace std;
int main()
{
int a[3];
int b[3];
int carry = 0;
int result[7];
a[0] = 1;
a[1] = 0;
a[2] = 0;
a[3] = 1;
b[0] = 1;
b[1] = 1;
b[2] = 1;
b[3] = 1;
for(int i = 0; i <= 3; i++)
{
if(a[i] + b[i] + carry == 0)
{
result[i] = 0;
carry = 0;
}
if(a[i] + b[i] + carry == 1)
{
result[i] = 0;
carry = 0;
}
if(a[i] + b[i] + carry == 2)
{
result[i] = 0;
carry = 1;
}
if(a[i] + b[i] + carry > 2)
{
result[i] = 1;
carry = 1;
}
}
for(int j = 0; j <= 7; j++)
{
cout<<result[j]<<" ";
}
system("pause");
}

Well, it is a pretty trivial problem.
How to add two binary numbers in c++. what is the logic of it.
For adding two binary numbers, a and b. You can use the following equations to do so.
sum = a xor b
carry = ab
This is the equation for a Half Adder.
Now to implement this, you may need to understand how a Full Adder works.
sum = a xor b xor c
carry = ab+bc+ca
Since you store your binary numbers in int array, you might want to understand bitwise operation.
You can use ^ for XOR,| operator for OR, & operator for AND.
Here is a sample code to calculate the sum.
for(i = 0; i < 8 ; i++){
sum[i] = ((a[i] ^ b[i]) ^ c); // c is carry
c = ((a[i] & b[i]) | (a[i] & c)) | (b[i] & c);
}

Since you were asking about C++, you deserve a C++ answer. Use bitsets:
#include <bitset>
#include <iostream>
int main() {
std::bitset<5> const a("1001");
std::bitset<5> const b("1111");
// m here is a mask to extract the lsb of a bitset.
std::bitset<5> const m("1");
std::bitset<5> result;
for (auto i = 0; i < result.size(); ++i) {
std::bitset<5> const diff(((a >> i)&m).to_ullong() + ((b >> i)&m).to_ullong() + (result >> i).to_ullong());
result ^= (diff ^ (result >> i)) << i;
}
std::cout << result << std::endl;
}
This works for arbitrarily long bit sets.

You could use "Bitwise OR" operation to reduce the code since
1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
You could also convert both number to decimal sum and them go back to binary again.
Converting decimal to binary
int toBinary (unsigned int num, char b[32])
{
unsigned int x = INT_MIN; // (32bits)
int i = 0, count = 0;
while (x != 0)
{
if(x & num) // If the actual o bit is 1 & 1 = 1 otherwise = 0
{
b[i] = '1';
count++;
}
else b[i] = '0';
x >>=1; // pass to the left
i++;
}
return count;
}

There is a bug :
if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
Also u might want to print in reverse
for(int j=6; j>=0; j--)
{
cout<<result[j]<<" ";
}

Your arrays are one item too small for your indexing.
int a[3] only has 3 elements, so a[3] = 1 is invalid (it has undefined behaviour) since it's accessing the 4th element, which doesn't exist.
Likewise for the other arrays.
That means that the whole program has undefined behaviour, i.e. it can do anything or nothing at all.
(What's probably happening in your case is that writing outside the arrays is overwriting the other variables.)
You're also not initialising the result array, so its content is just some random data.
Since you only update 4 of its elements but print all of them (and more), the output will be random data as well.

Following were the errors in your code and fixed code is also below"
int a[] was of size 3 so it cannot store at the 3rd index. use int a[4].
if(a[i]+b[i]+carry==1) wrong values were assigned in this check update result[i]=1; carry=0.
The sequence of checks is reversed.
The last carry was not stored in the result.
The addition result stored in the result array was in reverse order so printed it in reverse.
here is the working piece of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[4];
int b[4];
int carry=0;
int result[5];
a[0]=1;
a[1]=0;
a[2]=0;
a[3]=1;
b[0]=1;
b[1]=1;
b[2]=1;
b[3]=1;
for(int i=0; i<4; i++)
{
if(a[i]+b[i]+carry==3)
{
result[i]=1;
carry=1;
}
if(a[i]+b[i]+carry==2)
{
result[i]=0;
carry=1;
}
if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
if(a[i]+b[i]+carry==0)
{
result[i]=0;
carry=0;
}
}
result[4]=carry;
for(int j=4; j>=0; j--)
{
cout<<result[j];
}
cout<<endl;
return 0;
}

#include <stdio.h>
int main()
{
long binary1, binary2;
int i = 0, remainder = 0, sum[20];
printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
getch();
return 0;
}

Repeatedly do
(x, y) <- ((x & y) << 1, x ^ y)
until x is 0. y is the answer.

you should do this
for(int i = 3; i >= 0; i--)
{
if(a[i] + b[i] + carry == 0)
{
result[i] = 0;
carry = 0;
}
else if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
else if(a[i] + b[i] + carry == 2)
{
result[i] = 0;
carry = 1;
}
else if(a[i] + b[i] + carry > 2)
{
result[i] = 1;
carry = 1;
}
printf("%d",result[i]);
}

A non-conventional solution, but it works:
int main() {
int A[] = { 0, 0, 0, 1, 1, 0, 1, 0};
int B[] = { 0, 0, 0, 0, 1, 1, 0, 0};
int size = sizeof(A)/sizeof(*A);
int C[size+1];
int t = 0;
for(int i = size-1; i > -1; i--){
C[i+1] = A[i]+B[i]+t;
t = C[i+1]/2;
C[i+1] %= 2;
}
C[0] = t;
}

What if their sizes are not the same? Also, you would want to allow the user to input the binary numbers (in this case representing integers) as integers and not as elements of arrays. Here is a piece of code that accomplishes those :-)
#include <iostream>
using namespace std;
// Add two numbers in binary
void sumBinary(int num1, int num2, int* sum12){
int mod1 = 0;
int mod2 = 0;
int carry = 0;
int factor = 1;
int flag = 0;
*sum12 = 0;
while (!flag){
mod1 = num1 % 10;
mod2 = num2 % 10;
num1 /= 10;
num2 /= 10;
if ((carry + mod1 + mod2) == 2){
*sum12 += 0;
carry = 1;
}
else if ((carry + mod1 + mod2) == 3){
*sum12 += factor;
carry = 1;
}
else if ((carry + mod1 + mod2) == 0){
*sum12 += 0;
carry = 0;
}
else{
*sum12 += factor;
carry = 0;
}
factor *= 10;
if ((num1 == 0) && (num2 == 0)){
*sum12 += carry*factor;
flag = 1; }
}
}
void main(){
int num1, num2, sum12;
cout << "Enter the first binary integer number: ";
cin >> num1;
cout << "Enter the second binary integer number: ";
cin >> num2;
sumBinary(num1, num2, &sum12);
cout << "The sum in binary form is :" << sum12 << endl;
}

A simple way :
int getBit(string s, int index)
{
if(index >= 0) return (s[index] - '0');
else return 0;
}
string addBinary(string a, string b)
{
if(a.size() > b.size()) while(a.size() > b.size()) b = "0" + b;
else if(b.size() > a.size()) while(b.size() > a.size()) a = "0" + a;
int l = max(a.size()-1, b.size() - 1);
string result = "";
int s=0;
while(l>=0 || s==1)
{
s += getBit(a, l) + getBit(b, l) ;
result = char(s % 2 + '0') + result;
s /= 2;
l--;
}
return result;
}

int main(){
ios::sync_with_stdio(0); cin.tie(0);
int num1=12, num2=45, sum=0;
bool b1, b2, carry=0;
for(int i=0;i<32;i++){
b1=( 1<<i ) & num1;
b2=( 1<<i ) & num2;
sum = (b1 ^ b2 ^ carry) ? sum ^ (1<<i) : sum;
carry = ((b1 & b2) | (b1 & carry) | (b2 & carry));
}
cout<<sum;
return 0;
}

Easy to understand code
Add Two Binary Numbers (input datatype = int)
#include <iostream>
using namespace std;
int power(int a, int b)
{
int ans = 1;
while (b)
{
ans *= a;
b--;
}
return ans;
}
int main()
{
int n1, n2, carry = 0, ans = 0, rem1, rem2, remsum = 0, i;
cout << "Enter First Number : ";
cin >> n1;
cout << "Enter Second Number : ";
cin >> n2;
for (i = 0; n1 != 0 || n2 != 0; i++, n1 /= 10, n2 /= 10)
{
rem1 = n1 % 10;
rem2 = n2 % 10;
remsum = rem1 + rem2 + carry;
if (remsum == 2)
{
carry = 1;
remsum = 0;
}
else if (remsum == 3)
{
carry = 1;
remsum = 1;
}
else if (remsum == 0)
{
carry = 0;
remsum = 0;
}
else if (remsum == 1)
{
carry = 0;
remsum = 1;
}
ans = remsum * power(10, i) + ans;
}
ans = carry * power(10, i) + ans;
cout << ans;
return 0;
}

Related

multiples of x in a number

suppose we are given a range from l to r.We are asked to find count of that numbers whose all digits are multiples of x. x can be any number from 1 to 9. For example, take l=20 and r=40 and x=2 then required numbers are 20, 22, 24, 26, 28, 40 as multiples of x are 0,2,4,6,8.
I have written a code for that . It runs for some of the test case. I don't why it is giving wrong answer for most of the test case.
constraints : 1<=l<=r<=10^18.
my code :
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll s1(vector<string> &v, ll N)
{
ll i, j, n = v.size(), ans = 0;
string ss = "", s = "";
for (i = 0; i < n; i++)
{
s += v[i];
}
ss = to_string(N);
ll d = ss.length(), f = 0, x = n - 1, y = 1;
for (i = 1; i < d; i++)
{
if (i == 1)
{
ans += x;
y = x;
continue;
}
y = y * n;
ans += y;
}
ll z = 0;
for (j = 0; j < d; j++)
{
f = 0;
for (i = 0; i < s.length(); i++)
{
if (z == 0)
{
z++;
continue;
}
if (s[i] < ss[j])
{
ans += pow(n, d - (j + 1));
z++;
}
else if (s[i] == ss[j])
{
z++;
f = 1;
break;
}
}
if (!f)
{
break;
}
}
ans += f;
return and;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
ll l, r;
ll k, i, g;
cin >> l >> r >> k;
vector<ll> v1;
ll x = 0;
while (x <= 9)
{
v1.push_back(x);
x = x + k;
}
vector<string> s;
for (i = 0; i < v1.size(); i++)
{
g = v1[i];
char c = g + '0';
string d(1, c);
s.push_back(d);
// cout << D[i] << " ";
}
cout << s1(s, r) - s1(s, l - 1) << '\n';
}
}
Can anyone tell me the better logic for this question?
using dynamic programming we can write a function f(x,d) that gives us the amount of numbers in range [0,x] such that every digit is a multiple of d
dp states: [position in X][is our number prefix == X prefix?]
which will use log10(X) * 2 space
starting state is obviously [0][1]
then we just fill up the dp table, especially easy using recursion where we just check whats the next digit we can add that holds all conditions
So total complexity of such dp function would be O(log10(X) * 2 * 10) or simply O(log N) if we remove constants
then answer is simply f(R,x) - f(L-1,x)
Sample code (C++):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[22][2]; //[position in X][is our number prefix == X prefix?]
ll do_dp(vector<int>&digits, int k, int pos = 0, bool isEq = true)
{
if(pos >= digits.size())return 1;
if(dp[pos][isEq] != -1)return dp[pos][isEq];
dp[pos][isEq] = 0;
for(int d = 0; d <= (isEq ? digits[pos] : 9); d++) //d is next digit
if(d % k == 0) //multiple of k
dp[pos][isEq] += do_dp(digits, k, pos+1, isEq && d == digits[pos]);
return dp[pos][isEq];
}
ll solve(ll x, int k)
{
vector<int>digits;
while(x > 0){
digits.push_back(x%10);
x/=10;
}
reverse(digits.begin(),digits.end());
memset(dp, -1, sizeof dp);
return do_dp(digits, k);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll t,l,r,k;
cin>>t;
while(t--)
{
cin>>l>>r>>k;
cout<<solve(r,k) - solve(l-1,k)<<"\n";
}
}

C++ - Finding x Factor of a math expression

I'm trying to get the x factor of an input math expression, but it seems buggy.
Here is my code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
char a[100];
int res = 0; // final answer
int temp = 0; // factor of the x we are focused on - temporarily
int pn = 0; // power of 10 - used for converting digits to number
int conv; // used for conversion of characters to int
cout<< "Enter a: ";
cin>> a; //input expression
for(int i=0; i<100; i++){
// checking if the character is x - then get the factor
if(a[i]=='x'){
for(int j=1; j<=i+1; j++){
conv = a[i-j] - '0'; // conversion
if(conv>=0 && conv<=9){ // check if the character is a number
temp = temp + conv*pow(10, pn); // temporary factor - using power of 10 to convert digit to number
pn++; // increasing the power
}
else{
if(a[i-j]=='-'){
temp = -temp; // check if the sign is - or +
}
break;
}
if(i-j==0){
break;
}
}
res = res+temp; // adding the x factor to other ones
pn = 0;
temp = 0;
}
}
cout<< res;
return 0;
}
It doesn't work for some inputs, for example:
100x+3x gives 102 and 3x+3997x-4000x gives -1
But works for 130x and 150x!
Is there a problem with my code, or is there an easier way to do this?
I think you're not parsing the +. There may be some other problem I can't see in the original code, probably not. Here's the X-File:
int main(){
char a[100];
int res(0);
cout << "Enter a: ";
cin >> a;
for(int i = 0; i < 100; i++){
if(a[i] == 'x'){
int temp(0);
int pn(0);
for(int j = 1; j <= i; j++){
int conv = a[i - j] - '0';
if(conv >= 0 && conv <= 9){
temp += conv*pow(10, pn);
pn++;
} else if(a[i - j] == '-') {
temp = -temp;
break;
} else if(a[i - j] == '+') {
break;
} else {
// what to do...
}
}
res += temp;
}
}
cout << res;
return 0;
}

the biggest common divisor of 2 numbers using arrays

How could I find the biggest common divisor of 2 numbers using array? I tried to solve it using 2 arrays and I couldn't finish it. How could I improve this program?
#include <iostream>
using namespace std;
int main()
{
unsigned int A[2][10], B[2][10], a, b, c_exp, d, i1, P, x;
bool apartine = false;
cout << "a="; cin >> a;
cout << "b="; cin >> b;
P = 1;
c_exp = 0;
i1 = 0;
while (a % 2 == 0)
{
c_exp++;
a = a/2;
}
if (c_exp != 0)
{
A[i1][0] = 2;
A[i1][1] = c_exp;
i1++;
}
d = 3;
while (a != 1 && d <= a)
{
c_exp=0;
while (a % d == 0)
{
c_exp++;
a = a/d;
}
if (c_exp!=0)
{
A[i1][0] = d;
A[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea A contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << A[i][j] << ",";
}
c_exp = 0;
i1 = 0;
while (b % 2 == 0)
{
c_exp++;
b = b/2;
}
if (c_exp != 0)
{
B[i1][0] = 2;
B[i1][1] = c_exp;
i1++;
}
d = 3;
while (b != 1 && d <= b)
{
c_exp = 0;
while (b % d == 0)
{
c_exp++;
b = b/d;
}
if (c_exp != 0)
{
B[i1][0] = d;
B[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea B contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << B[i][j] << ",";
}
return 0;
}
From now on I have to find if the first number of first array exist in the second array and after this I have to compare the exponents of the same number of both array and the lowest one I have to add it to product. After this I have to repeat the same proccess with the second number to the last one of the first array. The problem is that I don't know how to write this.I have to mention that this program isn't complete.
Any ideas?
If you need better solution then you can avoid array and use the below logic.
int main()
{
int a =12 ,b = 20;
int min = a>b ? a:b; // finding minimum
if(min > 1)
{
for (int i=min/2; i>1; i--)//Reverse loop from min/2 to 1
{
if(a%i==0 && b%i==0)
{
cout<<i;
break;
}
}
}
else if(min == 1)
{
cout<<"GCD is 1";
}
else
cout<<"NO GCD";
return 0;
}
You can also check the working example Greatest Common Divisor
I am not quite sure what you are trying to achieve with your code. It looks over complicated. If I were to find the biggest common divisor of two numbers I would do something like the following:
## This is not a correct implementation in C++ (but close to it) ##
Read the two integers **a** and **b**
int max_div(int a, int b){
int div = a > b ? a : b;
while (div != 1 && (a%div != 0 && b%div != 0)){
div--;
}
return div;
}
This function starts with the minimum of a and b as the highest possible common divisor and then works its way backwards until one of two possible outcomes:
It finds a common divisor (a%div == 0 and b%div == 0)
It reaches one (always a common divisor)
EDIT : Now returns one if no higher divisor is found. (Was returning zero which made no sense)

Trouble with the carry over when adding 2 arrays together

I'm writing a program that adds two large integers (up to 20 digits) together. I've had no problems so far in storing the two numbers as strings then sorting them into two arrays.
So far, I have half of the addition part working. When the sum of the two digits does not exceed double digits, it works fine.
The issue arises when the sum of the arrays hits double digits. I'm trying to work in the carry over, but it messes with the digits (adding in where it shouldn't.) In addition to that, I'm not sure how to get the carry to appear ahead of the final digits. For example: 9+9 outputs to 8.
Here's my code (please excuse all the letter variables in the for loops.)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
string str2;
int array1[20];
int array2[20];
int array3[20];
string num3[20];
int i;
int j = 0;
int k;
int l;
int m = 0;
int n;
int o;
int carry = 0;
cout<<"Please enter the first number: "<<endl;
cin>>str1;
for (int i = str1.length() - 1; i >= 0; i--)
{
array1[j] = str1[i];
j++;
}
for (int k = str1.length()-1; k >=0; k--)
{
array1[k] = static_cast<int>(str1[k]) - static_cast<int>('0');
}
cout<<"Please enter the second number: "<<endl;
cin>>str2;
for (int l = str2.length() - 1; l >= 0; l--)
{
array2[m] = str2[l];
m++;
}
for (int n = str2.length()-1; n >=0; n--)
{
array2[n] = static_cast<int>(str2[n]) - static_cast<int>('0');
}
//Where the addition begins
for (int o = 0; o < str1.length(); o++)
{
if (array1[o] + array2[o] > 9)
{
array3[o] = array1[o] + array2[o] + carry;
array3[o] = array3[o] % 10;
carry = 1;
}
else
{
array3[o] = array1[o] + array2[o] + carry;
carry = 0;
}
cout<<array3[o];
}
return 0;
}
I think one thing I have to fix is how this line of code works:
array3[o] = array3[o] % 10;
Which keeps a second digit from appearing in the output. I would imagine if I disabled it once we reach the final numbers in the arrays, it would allow the final carry to show up. Unfortunately, everything I've tried hasn't worked.
Again, thank you!
Try this:
for (int o = 0; o < str1.length(); o++)
{
if (array1[o] + array2[o] + carry > 9)
{
array3[o] = array1[o] + array2[o] + carry;
array3[o] = array3[o] % 10;
carry = 1;
}
else
{
array3[o] = array1[o] + array2[o] + carry;
carry = 0;
}
cout<<array3[o];
}
Modify your for loop for Addition. In condition you need to add carry also
if (array1[o] + array2[o] + carry > 9)
The final for loop will be as below:
for (int o = 0; o < str1.length(); o++)
{
if (array1[o] + array2[o] + carry > 9)
{
array3[o] = array1[o] + array2[o] + carry;
array3[o] = array3[o] % 10;
carry = 1;
}
else
{
array3[o] = array1[o] + array2[o] + carry;
carry = 0;
}
cout<<array3[o];
}
My suggestions:
You can fill up the numbers from the input string in one loop. No need to use two loops.
for (int i = str1.length() - 1; i >= 0; i--)
{
array1[j] = str1[i] - '0';
j++;
}
Similarly for the other loop.
When computing the total you have iterate until the length of the longest string. If the first input is 12 and the second input is 4567, you have to make sure that your iteration stops at 4, not at 2.
The algorithm for computing the sum can be simplified to:
for (int o = 0; o < len+1; o++)
{
array3[o] = array1[o] + array2[o] + carry;
carry = array3[o]/10;
array3[o] %= 10;
}
where len is the maximum of the lengths.
Here's the final code I came up with:
#include <iostream>
#include <string>
using namespace std;
void printNumber(int array[])
{
// Skip the leading zeros.
int i = 19;
for ( ; i >= 0; i-- )
{
if ( array[i] > 0 )
{
break;
}
}
for ( ; i >= 0; i--)
{
cout << array[i];
}
}
int main()
{
string str1;
string str2;
int array1[20] = {0};
int array2[20] = {0};
int array3[20] = {0};
int i;
int j = 0;
int k;
int l;
int m = 0;
int n;
int o;
int carry = 0;
int len = 0;
cout<<"Please enter the first number: "<<endl;
cin>>str1;
len = str1.length();
for (int i = str1.length() - 1; i >= 0; i--)
{
array1[j] = str1[i] - '0';
j++;
}
cout<<"Please enter the second number: "<<endl;
cin>>str2;
if ( len < str2.length() )
{
len = str2.length();
}
for (int l = str2.length() - 1; l >= 0; l--)
{
array2[m] = str2[l] - '0';
m++;
}
//Where the addition begins
for (int o = 0; o < len+1; o++)
{
array3[o] = array1[o] + array2[o] + carry;
carry = array3[o]/10;
array3[o] %= 10;
}
// Print the result.
printNumber(array3);
cout << endl;
return 0;
}
int main()
{
char A[20],B[20],C[22]={0};
int carry,len_a,len_b,x=20,i,j,a,b;
printf("First Number");
gets(A);
printf("Second Number");
gets(B);
len_a=strlen(A);
len_b=strlen(B);
for(i=len_a-1;i>=0;i--)
{
carry=0;
b=(int)B[i]-48;
a=(int)A[len_b-1]-48;
C[x]=C[x]+a+b;
if(C[x]>9)
{
C[x]=C[x]%10;
C[x-1]+=1;
}
x--;
len_b--;
}
int flag=0;
printf("Result :");
for(j=0;j<=20;j++)
{
if(C[j]!=0)
{
printf("%d",C[j]);
flag=1;
}
else if(C[j]==0 && flag==1)
printf("%d",C[j]);
}
if(flag==0)
printf("0");
getch();
return 0;
}
If I were you, would do exactly what I have done here:
inline bigint &bigint::operator+( const bigint & _expr )
{
vector<uint8_t> left = this->_digits;
vector<uint8_t> right = _expr._digits;
vector<uint8_t> sum;
uint8_t carry = 0;
process_operands( left, right ); // makes the two operands have the same length and fills them with leading zeros
for( auto lit = left.cbegin(), rit = right.cbegin(); lit != left.cend(), rit != right.cend(); ++lit, ++rit )
{
uint8_t temp_sum = ( *lit + *rit + carry ) % 10;
carry = ( *lit + *rit + carry ) / 10;
sum.push_back( temp_sum );
}
if( carry ) sum.push_back( carry );
this->_digits = sum;
return *this;
}
To make things look a little bit more clear:
bigint is my class for big integers, and looks something like this:
class bigint
{
private:
vector<uint8_t> _digits;
typedef vector<uint8_t>::size_type size_type;
bigint( vector<uint8_t> & in );
public:
bigint() : _digits() {}
bigint( const string &number );
// ...
};
So you should actually stop using the built-in arrays, since they are error-prone, and because we have better things offered by STL, like std::vector. I am using std::vector<uint8_t> to store the digits of my number, and so, it becomes easier to cycle through the digits: we can use either the range for (for(uint8_t & c : _digits) { }) or the iterators.
Attaching the leading zeros will become easier, since you ony have to do:
_digits.push_back( 0 );
in a for loop.

long integer multiplication

I am preparing the interview questions not for homework. There is one question about how to multiple very very long integer. Could anybody offer any source code in C++ to learn from? I am trying to reduce the gap between myself and others by learning other's solution to improve myself.
Thanks so much!
Sorry if you think this is not the right place to ask such questions.
you can use GNU Multiple Precision Arithmetic Library for C++.
If you just want an easy way to multiply huge numbers( Integers ), here you are:
#include<iostream>
#include<string>
#include<sstream>
#define SIZE 700
using namespace std;
class Bignum{
int no[SIZE];
public:
Bignum operator *(Bignum& x){ // overload the * operator
/*
34 x 46
-------
204 // these values are stored in the
136 // two dimensional array mat[][];
-------
1564 // this the value stored in "Bignum ret"
*/
Bignum ret;
int carry=0;
int mat[2*SIZE+1][2*SIZE]={0};
for(int i=SIZE-1;i>=0;i--){
for(int j=SIZE-1;j>=0;j--){
carry += no[i]*x.no[j];
if(carry < 10){
mat[i][j-(SIZE-1-i)]=carry;
carry=0;
}
else{
mat[i][j-(SIZE-1-i)]=carry%10;
carry=carry/10;
}
}
}
for(int i=1;i<SIZE+1;i++){
for(int j=SIZE-1;j>=0;j--){
carry += mat[i][j]+mat[i-1][j];
if(carry < 10){
mat[i][j]=carry;
carry=0;
}
else{
mat[i][j]=carry%10;
carry=carry/10;
}
}
}
for(int i=0;i<SIZE;i++)
ret.no[i]=mat[SIZE][i];
return ret;
}
Bignum (){
for(int i=0;i<SIZE;i++)
no[i]=0;
}
Bignum (string _no){
for(int i=0;i<SIZE;i++)
no[i]=0;
int index=SIZE-1;
for(int i=_no.length()-1;i>=0;i--,index--){
no[index]=_no[i]-'0';
}
}
void print(){
int start=0;
for(int i=0;i<SIZE;i++)
if(no[i]!=0){
start=i;
break; // find the first non zero digit. store the index in start.
}
for(int i=start;i<SIZE;i++) // print the number starting from start till the end of array.
cout<<no[i];
cout<<endl;
return;
}
};
int main(){
Bignum n1("100122354123451234516326245372363523632123458913760187501287519875019671647109857108740138475018937460298374610938765410938457109384571039846");
Bignum n2("92759375839475239085472390845783940752398636109570251809571085701287505712857018570198713984570329867103986475103984765109384675109386713984751098570932847510938247510398475130984571093846571394675137846510874510847513049875610384750183274501978365109387460374651873496710394867103984761098347609138746297561762234873519257610");
Bignum n3 = n1*n2;
n3.print();
return 0;
}
as you can see, it's multiply 2 huge integer :) ... (up to 700 digits)
Try this:
//------------DEVELOPED BY:Ighit F4YSAL-------------
#include<iostream>
#include<string>
#include<sstream>
#define BIG 250 //MAX length input
using namespace std;
int main(){
int DUC[BIG][BIG*2+1]={0},n0[BIG],n1[BIG],i,t,h,carry=0,res;
string _n0,_n1;
while(1){
//-----------------------------------get data------------------------------------------
cout<<"n0=";
cin>>_n0;
cout<<"n1=";
cin>>_n1;
//--------------------string to int[]----------------------------------------
for(i=_n0.length()-1,t=0;i>=0,t<=_n0.length()-1;i--,t++){
n0[i]=_n0[t]-'0';
}
i=0;
for(i=_n1.length()-1,t=0;i>=0,t<=_n1.length()-1;i--,t++){
n1[i]=_n1[t]-'0';
}
i=0;t=0;
//--------------------------produce lines of multiplication----------------
for(i=0;i<=_n1.length()-1;i++){
for(t=0;t<=_n0.length()-1;t++){
res=((n1[i]*n0[t])+carry);
carry=(res/10);
DUC[i][t+i]=res%10;
}
DUC[i][t+i]=carry;
carry=0;
}
i=0;t=0;res=0;carry=0;
//-----------------------------add the lines-------------------------------
for(i=0;i<=_n0.length()*2-1;i++){
for(t=0;t<=_n1.length()-1;t++){
DUC[BIG-1][i]+=DUC[t][i];
//cout<<DUC[t][i]<<"-";
}
res=((DUC[BIG-1][i])+carry);
carry=res/10;
DUC[BIG-1][i]=res%10;
//cout<<" ="<<DUC[BIG-1][i]<<endl;
}
i=0;t=0;
//------------------------print the result------------------------------------
cout<<"n1*n0=";
for(i=_n0.length()*2-1;i>=0;i--){
if((DUC[BIG-1][i]==0) and (t==0)){}else{cout<<DUC[BIG-1][i];t++;}
//cout<<DUC[BIG-1][i];
}
//-------------------------clear all-------------------------------------
for(i=0;i<=BIG-1;i++){
for(t=0;t<=BIG*2;t++){
DUC[i][t]=0;
}
n0[i]=0;n1[i]=0;
}
//--------------------------do it again-------------------------------------
cout<<"\n------------------------------------------------\n\n";
}
return 0;
}
This solution is good for very very big numbers but not so effective for factorial calculation of very big numbers. Hope it will help someone.
#include <iostream>
#include <string>
using namespace std;
string addition(string a, string b) {
string ans = "";
int i, j, temp = 0;
i = a.length() - 1;
j = b.length() - 1;
while (i >= 0 || j >= 0) {
if (i >= 0 && a[i])
temp += a[i] - '0';
if (j >= 0 && b[j])
temp += b[j] - '0';
int t = (temp % 10);
char c = t + '0';
ans = ans + c;
temp = temp / 10;
i--;
j--;
}
while (temp > 0) {
int t = (temp % 10);
char c = t + '0';
ans = ans + c;
temp = temp / 10;
}
string fnal = "";
for (int i = ans.length() - 1;i >= 0;i--) {
fnal = fnal + ans[i];
}
return fnal;
}
string multiplication(string a, string b) {
string a1, b1 = "0";
int i, j, temp = 0, zero = 0;
i = a.length() - 1;
int m1, m2;
while (i >= 0) {
a1 = "";
m1 = a[i] - '0';
j = b.length() - 1;
while (j >= 0) {
m2 = b[j] - '0';
temp = temp + m1*m2;
int t = temp % 10;
char c = t + '0';
a1 = a1 + c;
temp = temp / 10;
j--;
}
while (temp > 0) {
int t = (temp % 10);
char c = t + '0';
a1 = a1 + c;
temp = temp / 10;
}
string fnal = "";
// reverse string
for (int i = a1.length() - 1;i >= 0;i--) {
fnal = fnal + a1[i];
}
a1 = fnal;
//add zero
for (int p = 0;p < zero;p++)
a1 = a1 + '0';
b1 = addition(a1, b1);
i--;
zero++;
}
return b1;
}
// upto 50 is ok
int factorial(int n) {
string a = "1";
for (int i = 2;i <= n;i++) {
string b = to_string(i);
a = multiplication(a, b);
}
cout << a << endl;
return a.length();
}
int main() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int n;
cin >> n;
//cout << factorial(n) << endl;
string a, b;
a = "1281264836465376528195645386412541764536452813416724654125432754276451246124362456354236454857858653";
b = "3767523857619651386274519192362375426426534237624548235729562582916259723586347852943763548355248625";
//addition(a, b);
cout << multiplication(a, b);
return 0;
}