Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do i modify my code so that when adding negative numbers, it actually subtracts them. Here is my add function: BigInteger BigInteger::add ( const

how do i modify my code so that when adding negative numbers, it actually subtracts them. Here is my add function:
BigInteger BigInteger::add(const BigInteger& N) const {
// Initializing variables to store the result and carry
BigInteger result;
int carry =0;
// Create copies of the List objects to avoid modifying the originals
List thisDigits = digits;
List NDigits = N.digits;
// Initialize cursors for both BigIntegers
thisDigits.moveBack();
NDigits.moveBack();
// 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.movePrev();
}
// Check if the current digit of N is negative
if (NDigits.position()!=0 && N.signum ==-1){
sum -= NDigits.movePrev(); // Subtract the absolute value
} else if (NDigits.position()!=0){
sum += NDigits.movePrev();
}
// Update the carry for the next iteration
carry = sum / base; // Calculate the new carry based on the current sum
sum %= base; // Ensure that sum is within the base range
// Add the total to the result
result.digits.insertBefore(sum);
}
// Check if the sum is zero
if (result.digits.length()==1 && result.digits.front()==0){
result.makeZero();
return result;
}
// Set the sign of the result based on the signs of the operands
if (signum == N.signum){
// If signs are the same, result has the same sign
result.signum = signum;
} else {
// If signs are different
int comparison = compare(N);
if (comparison ==0){
// If magnitudes are equal, result is zero
result.makeZero();
return result; // Return zero result immediately
} else if (comparison >0){
// If this has greater magnitude, result is positive
result.signum = signum;
} else {
// If N has greater magnitude, result is negative
result.signum = N.signum;
}
}
return result;
}
here is some pseudocide that could help, though I'm having trouble following it:
Function add(N):
Initialize result
Initialize carry
// Initialize cursors for both operands
Initialize cursors for this and N
// Iterate through digits until both operands are processed
While this or N or carry is not zero:
Initialize sum as carry
// Add digits from this operand
If this digit is not zero:
Add this digit to sum
// If the current digit of N is negative, treat it as subtraction
If N signum is negative:
Subtract the absolute value of N digit from sum
Else:
Add N digit to sum
// Update carry
Set carry based on sum
// Add the total to the result
Add sum mod base to result
// Set the sign of the result based on the signs of the operands
Set result sign based on the signs of this and N
Return result

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago