Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The \"this\" object gets a new value, the sum of its original value plus the par

ID: 3538953 • Letter: T

Question

The "this" object gets a new value, the sum of its original value plus the parameter's value.

If the vector in the "this" object is smaller than the vector in the parameter object, resize the "this" object's vector to the same size as the parameter object's vector.

Use the everyday addition algorithm, add the ones column, the tens column, hundreds column, ... . Don't forget the carry.

Use an if statement (or ?:) to get the digit from the parameter object or 0, don't access a vector out of bounds. The "this" object's vector will have room (why?).

The carry may be nonzero after the loop, consider 912 + 135.


I need to add 2 int vectors:
BigInt& operator+=(const BigInt&)

The "this" object gets a new value, the sum of its original value plus the parameter's value.

If the vector in the "this" object is smaller than the vector in the parameter object, resize the "this" object's vector to the same size as the parameter object's vector.

Use the everyday addition algorithm, add the ones column, the tens column, hundreds column, ... . Don't forget the carry.

Use an if statement (or ?:) to get the digit from the parameter object or 0, don't access a vector out of bounds. The "this" object's vector will have room (why?).

The carry may be nonzero after the loop, consider 912 + 135.

Explanation / Answer

BigInt& BigInt :: operator+=(const BigInt& n)

{

vector<int> sum;

int size1 = (*this).size();

int size2 = n.size();

int finalSize = (size1>size2)?size1:size2; //final size of the vector

  

//Simply adding digit by digit

for(int i=0;i<finalSize;i++)

{

if(i>=size1)

{

sum.push_back(n[i]);

}

else if(i>=size2)

{

sum.push_back(num[i]);

}

else

{

sum.push_back(num[i]+n[i]);

}

}


//Managing the carry over

for(int i=1;i<sum.size();i++)

{

sum[i] = sum[i]+sum[i-1]/10;

sum[i-1]%=10;

}


//if final last digit is > 9

if(sum[sum.size()-1]>9)

{

sum.push_back(sum[sum.size()-1]/10);

sum[sum.size()-2]%=10;

}

  

this->num = sum;

return *this;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote