Question: how to modify this add function to correctly add pos and negatives, not just positives: BigInteger BigInteger::add ( const BigInteger& N ) const { /
how to modify this add function to correctly add pos and negatives, not just positives: BigInteger BigInteger::addconst BigInteger& N const
Initialize variables to hold the result and carry
BigInteger result;
int carry ;
Create copies of the List objects to avoid modifying the originals
List thisDigits digits;
List NDigits Ndigits;
Initialize cursors for both BigIntegers
thisDigits.moveBack; Move to the back to start from the end
NDigits.moveBack;
Determine the sign of the result
int resultSign signum;
Iterate through the digits of both BigIntegers
while thisDigitsposition NDigits.position carry
Calculate the sum of digits plus carry
int sum carry;
if thisDigitsposition
sum thisDigits.peekPrev;
thisDigits.movePrev;
if NDigitsposition
sum NDigits.peekPrev;
NDigits.movePrev;
Handle the case when the result sign chang
if resultSign
resultSign sum : ;
Update carry and prepend the sum digit to the result
carry sum base;
result.digits.insertAftersum base;
Set the sign of the result
result.signum resultSign;
return result;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
