Subtraction of two integers

Hey guys.
I am working on an assignment where I have to subtract two very large integers. How can I write it's code provided that I have both numbers in a character array with each index holding a fraction of the original number. Like if I have a number 123456789 in an array then
arr[0]='1';
arr[1]='2';
arr[2]='3';
arr[3]='4';
and so on. now if i have to subtract this number from an other number, how can I do that?
How would you solve this by hand?

1234 - 763

4 - 3 = 1
current result: 0001

3 - 6, 6 > 3 so we must subtract one from the digit on left and add ten to it

12 becomes 11
13-6 = 7
current result: 0071
1 - 7, 7 > 1 so we must subtract one from the digit on the left and add ten to it
1 becomes 0
11 - 7 = 4
current result: 0471

0 - ? = 0 (there is nothing left on the second operand so just prepend everything left)

final result: 0471 or 471.

yeah that was what I thought of earlier, but the problem is that I have a very long integer on my hand.. like 10000000000000000000000000000000000000000003 - 568
give me an idea of how can I keep track of my borrow number?
If the number is 0 then move one to the left until it finds a positive number.

You basically have the position of the current digit.


so

10000000000000000000000000000000000000000003 - 568

Position = 0

first[0] = 3
second[0] = 8

Basically you want to find the first number greater than 0. Then subtract 1 from that number and make all the 0's between that number and the current position a 9.

So that would mean the first number greater than 0 would be: 1 at position 43ish?

So now we must move it over to the right by adding 10 and subtracting 1 (making them 9 and adding 10 to current position) which results in:
09999999999999999999999999999999999999999993 The 3 is now a 13 really so we can do:

13 - 8 = 5

Now we have 0999999999999999999999999999999999999999999 - 56 = 005

9 - 6 = 3

now we have 099999999999999999999999999999999999999999 - 5 = 035

9 - 5 = 4

now we have 09999999999999999999999999999999999999999 - ? = 435

now we are out of bounds on second number so we must prepend everything which results in: 09999999999999999999999999999999999999999435
09999999999999999999999999999999999999999435 <--confirmed at http://web2.0calc.com/

Funny I tried confirming with the calculator that comes with windows and was thinking wow I am really bad at math because it was saying the result was: 9999999999999999999999999999432 but I just couldn't think of a way how they ended with such little digits and a 2 as first digit. Then I jumped to conclusion it couldn't handle the large number.
You should make a variable that holds your remainder. I remember doing a program like this when I first started to learn C++. Here is a link that can help just be sure to follow the program closely.
Take a simple arithmetic problem: what's left over when you divide 11 by 3? The answer is easy to compute: divide 11 by 3 and take the remainder: 2. But how would you compute this in a programming language like C or C++? It's not hard to come up with a formula, but the language provides a built-in mechanism, the modulus operator ('%'), that computes the remainder that results from performing integer division.




Example: http://www.cprogramming.com/tutorial/modulus.html
Topic archived. No new replies allowed.