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

I need help finishing this portion of my code: BigInt& BigInt::operator+=(const

ID: 3539250 • Letter: I

Question

I need help finishing this portion of my code:


BigInt& BigInt::operator+=(const BigInt& num2)
{
if(_digits.size()<num2._digits.size())
    _digits.resize(num2._digits.size(),0);

    for(int idx=0; idx<_digits.size(); idx++)
    {
      int right_digit;
      if (idx < num2._digits.size())
        right_digit = num2._digits[idx];
      else
        right_digit = 0;                                                      
      // I need help here. Each one of these indexes hold a single int        
      // It needs to loop through and add all of the index values              
      // If the value of the sum is greater than 9                             
      // Then a carry must be applied to the next sum                          
      // After the last two ints are added and there is a carry                
      // Then the _digits vector needs to be increased by one and              
      // The carry will be placed in this index.                               
      int carry = 0;
      _digits[idx] = _digits[idx]+right_digit+carry;

    }


}

Explanation / Answer

You just need a "for" statement so it continues to loop, actually I would use a while statement unless you want it to loop forever. Inside the "for statement, write the variables to you wish to add the index values to, it will automatically add them once it reaches that line of code each ime it loops.


For a carry to be applied to the next sum you must determine what it is and if its a decimal number make sure to use a double variable so you don't leave out the decimals.

All you really need is a for statement and two if statments maybe 3. But it really comes down to how long you want it to loop for. If its just temporary use a while statement and each time it loops add 1 to a variable, when that variable reaches a certain value you exit out of it.

For example

int x =0;

while (x<10)

{

do all your stuff here...

x=x+1;

}

This will cause it to loop only 10 times.


Good luck.


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