I have a slight homework problem that is giving me some trouble. We have to write the algorithm for subtraction using different bases as a possibility in a program that uses calculator registers. The following is the provided prototype:

bool subtractRegisters( int registerX[], int registerY[], int registerZ[], int base );

The Y is taken from the X and placed in Z. I have completed correctly the whole subtraction algorithm involving the base calculation. I am just very confused on what is being returned as the boolean value. I much appreciate the help anyone familiar with this algorithm can provide.

7 Years
Discussion Span
Last Post by vmanes

Perhaps a success/failure indicator? Are you to provide negative results if Y is larger value than X, or should the operation not proceed in that case?

In other words, is the value in Z to be considered a valid result or not.


I have the overflow and negative possibilities handled by returning false now, and I now realized that I'm not sure where to return true. These are the instructions for the subtraction algorithm, but im not sure about the italicized part about returning true:

The subtraction algorithm also starts at the LSD point of the array and iterates toward the higher indices. Before you can subtract, you must check to make sure the subtraction will work. The algorithm for borrowing is to add the base to the present minuend (i.e., the value subtracted from) and then search in the direction of the MSD for a value to reduce by one; if the next value over is greater than zero, it will be decremented by one and the subtraction can continue. If the next value over is zero, or more values beyond are zeroes, you must consistently change the zeroes to one less than the base (i.e., base - 1) until you come to a value you can decrement. If you run out of digits before you find a value to borrow from, your process has underflowed and you should report this by returning false. Otherwise, if things go well, you can return true later. Finally, if the borrowing process was needed and succeeded, or if the minuend was already greater than the subtrahend, you can subtract the subtrahend from the minuend and store the result in the answer register. Note that the above actions cause a change in the minuend, which is generally not acceptable when implementing subtraction. This means you will need to work with a copy of the minuend; this will be addressed later in this document.

I'm not positive about how to know how to flag the program when it is at the end of an LSD'ed array in the Xregister. I can feel something at the tip of my forehead that is just whispering the answer, but I can't quite get the handle on the problem of this return.


It pays to work it out by hand

-   99

for the lowest order digit, you start by borrowing from the tens place, thus you assume value 10 in the ones and, oh, the tens is a zero, so assign it 9 (borrowed a hundred from the hundreds, moved ten of that to the ones). So far, so good. Do the subtraction of of the ones column, move to the tens column, repeat. Then the hundreds, and you run out of digits in the subtrahend. You would return true from this example. Just have a return true statement at the end of the function - if you find yourself completing the process with no error, that's the exit point.

- 1234

proceeds as before, but when you get to subtracting the subtrahend's thousand digit, there's a 0 in the minuend's place. So, you could clear the result register, or leave it, and your function returns false indicating bad result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.