Question: Help me fix this code: BigInteger BigInteger::add ( const BigInteger& N ) const { / / Initializing variables to store the result and carry BigInteger
Help me fix this code: BigInteger BigInteger::addconst BigInteger& N const
Initializing variables to store the result and carry
BigInteger result;
int carry ;
Creating 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;
For finding out the sign of the result
int resultSign signum;
Scroll through the digits of the two BigInteger numbers one by one
while thisDigitsposition NDigits.position carry
Calculating sum of digits plus carry
int sum carry;
if thisDigitsposition
sum thisDigits.peekPrev;
thisDigits.movePrev;
if NDigitsposition
sum NDigits.peekPrev;
NDigits.movePrev;
Debugging: Print sum, carry, and intermediate result
std::cout "Sum: sum Carry: carry std::endl;
std::cout "Intermediate Result: result.tostring std::endl;
Update the carry and add the total to the result
carry sum base;
result.digits.insertAfterabssum base;
Set the sign of the result
result.signum resultSign;
Changing the sign based on result
if resultSign
result.signum carry : ;
else if carry && result.signum
result.signum result.signum;
else if carry && result.signum
result.signum result.signum;
return result;
Based on this output: Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Sum: Carry:
Intermediate Result:
Test Addtest: FAILED: test
This is teh test case: case Addtest:
Adding numbers fall into one of cases, denote pos positive number,
neg negative number
pos pos pos
pos neg
neg pos
neg neg neg
A BigInteger;
B BigInteger;
pos pos pos
D BigInteger;
C A B;
if C D
return ;
add a positive and a negative integer
pos neg
B BigInteger;
C A B;
if Csign
return ;
pos neg
B BigInteger;
D BigInteger;
C A B;
if Csign
return ;
if C D
return ;
pos neg
B BigInteger;
D BigInteger;
C A B;
if Csign
return ;
if C D
return ;
neg neg neg
A BigInteger;
D BigInteger;
C A B;
if C D
return ;
return ;
Im currently passing teh first case and failing all the rest.
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
