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::add(const BigInteger& N) const {
// Initializing variables to store the result and carry
BigInteger result;
int carry =0;
// Creating copies of the List objects to avoid modifying the originals
List thisDigits = digits;
List NDigits = N.digits;
// 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 (thisDigits.position()!=0|| NDigits.position()!=0|| carry !=0){
// Calculating sum of digits plus carry
int sum = carry;
if (thisDigits.position()!=0){
sum += thisDigits.peekPrev();
thisDigits.movePrev();
}
if (NDigits.position()!=0){
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.to_string()<< std::endl;
// Update the carry and add the total to the result
carry = sum / base;
result.digits.insertAfter(abs(sum)% base);
}
// Set the sign of the result
result.signum = resultSign;
// Changing the sign based on result
if (resultSign ==0){
result.signum =(carry <0)?-1 : 1;
} else if (carry <0 && result.signum >0){
result.signum = result.signum;
} else if (carry >0 && result.signum <0){
result.signum = result.signum;
}
return result;
}
Based on this output: Sum: 3, Carry: 0
Intermediate Result: 0
Sum: 3, Carry: 0
Intermediate Result: 3
Sum: 3, Carry: 0
Intermediate Result: 33
Sum: 3, Carry: 0
Intermediate Result: 333
Sum: 3, Carry: 0
Intermediate Result: 3333
Sum: 3, Carry: 0
Intermediate Result: 33333
Sum: 3, Carry: 0
Intermediate Result: 333333
Sum: 3, Carry: 0
Intermediate Result: 3333333
Sum: 3, Carry: 0
Intermediate Result: 33333333
Sum: 3, Carry: 0
Intermediate Result: 333333333
Sum: 3, Carry: 0
Intermediate Result: 3333333333
Sum: 3, Carry: 0
Intermediate Result: 33333333333
Sum: 2, Carry: 0
Intermediate Result: 0
Sum: 2, Carry: 0
Intermediate Result: 2
Sum: 2, Carry: 0
Intermediate Result: 22
Sum: 2, Carry: 0
Intermediate Result: 222
Sum: 4, Carry: 0
Intermediate Result: 2222
Sum: 4, Carry: 0
Intermediate Result: 42222
Sum: 4, Carry: 0
Intermediate Result: 442222
Sum: 4, Carry: 0
Intermediate Result: 4442222
Sum: 6, Carry: 0
Intermediate Result: 44442222
Sum: 6, Carry: 0
Intermediate Result: 644442222
Sum: 6, Carry: 0
Intermediate Result: 6644442222
Sum: 6, Carry: 0
Intermediate Result: 66644442222
Test Add_test: FAILED: test 2
This is teh test case: case Add_test: {
/*
* Adding numbers fall into one of 4 cases, denote pos = positive number,
* neg = negative number
*
* pos + pos = pos
*
* pos + neg =0
*<0
*>0
*
* neg + pos =0
*<0
*>0
*
* neg + neg = neg
*/
A = BigInteger("+111122223333");
B = BigInteger("+222211110000");
// pos + pos = pos
D = BigInteger("+333333333333");
C = A + B;
if (!(C == D))
return 1;
// add a positive and a negative integer
//-> pos + neg =0
B = BigInteger("-111122223333");
C = A + B;
if (C.sign()!=0)
return 2;
//-> pos + neg >0
B = BigInteger("-110122223333");
D = BigInteger("1000000000");
C = A + B;
if (C.sign()!=1)
return 31;
if (!(C == D))
return 32;
//-> pos + neg <0
B = BigInteger("-112122223333");
D = BigInteger("-1000000000");
C = A + B;
if (C.sign()!=-1)
return 41;
if (!(C == D))
return 42;
//-> neg + neg = neg
A = BigInteger("-221211110000");
D = BigInteger("-333333333333");
C = A + B;
if (!(C == D))
return 5;
return 0;
}
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!