adding base n numbers

This is a discussion on adding base n numbers within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a function that adds together two numbers that are in an arbitrary base. So far ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Question adding base n numbers

    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
    Attached Files Attached Files

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Convert to base 10, add the numbers, convert back to base n.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    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

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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
    result variable, and add 1 to the second last digit addition.

    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.
    Add it as the second last digit in the answer. Add 1 to the next
    digit addition.
    
       1
       246
    + 2161   Sum: 27
    
    1 + 2 + 1 = 4, not greater than 8. Add it as the third last digit in
    the answer.
    
       246
    + 2161   Sum: 427
    
    0 + 2 = 2, not greater than 8. Add it as the fourth last digit in the
    answer.
    
       246
    + 2161   Sum: 2427
    
    No more digits, the answer has been given: 2427
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    strtol() and radix are your friend, but it only handles up to base 36.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Base 10 numbers to Base 2, 8, and 16
    By killcapital in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 11:05 AM
  2. How do you represent non-integer base numbers?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 04-08-2004, 02:09 PM
  3. Adding up all the numbers in the list...
    By Grayson_Peddie in forum C# Programming
    Replies: 0
    Last Post: 06-04-2003, 12:57 PM
  4. Replies: 11
    Last Post: 11-12-2001, 05:25 PM
  5. converting numbers from one base to another
    By partnole in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2001, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21