• 11-11-2002
doogle
:confused: I am trying to write a function that adds together two numbers that are in an arbitrary base.

So far i have managed to convert arbitrary base n numbers but the function to add them together is causing me problems.

Anyone got any ideas?! It's getting quite urgent!

At the moment the function is:

int addBaseN( BaseNnum sum, BaseNnum a, BaseNnum b, int base )

where sum, a and b are arrays and base is an int.

If anyone can help me out i would be most appreciative! I have attached the program so people can see where I am going!

cheers
• 11-11-2002
Magos
Convert to base 10, add the numbers, convert back to base n.
• 11-11-2002
doogle
cheers but the problem with that is that i want to be able to add huge numbers together i.e arbitrarily large so i want to be able to add then whilst they are in base n
• 11-11-2002
Magos
¤ Add the last elements in the variables.

¤ If the sum is greater than or equal to the base, subtract the
base from the sum, place the remainder as the last digit in the

¤ Otherwise, simply place the sum as the last digit.

¤ Repeat from ¤1 (assuming the second last digit is now the last).

Example (Two numbers in the base 8):
Code:

246
+ 2161  Sum:

6 + 1 = 7, not greater than 8. Add it as the last digit in the answer.

246
+ 2161  Sum: 7

4 + 6 = 10, which is greater than 8. Subtract 8 and you get 2.

1
246
+ 2161  Sum: 27

1 + 2 + 1 = 4, not greater than 8. Add it as the third last digit in

246
+ 2161  Sum: 427

0 + 2 = 2, not greater than 8. Add it as the fourth last digit in the

246
+ 2161  Sum: 2427

No more digits, the answer has been given: 2427

• 11-11-2002
moi
strtol() and radix are your friend, but it only handles up to base 36.