Thread: Dynamic array question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Dynamic array question

    How to write functions to add 2 big integers with dynamic arrays?
    The 2 integers are input by users.
    2 integer arrays store the input such that :
    x[0]=number of digits
    x[1]=1 st digit of the integer input (starting from the right)
    x[2]=2 nd digit of the integer input
    ... & so on
    eg.
    input integer 1: 456
    x[0]=3
    x[1]=6
    ...

    There is function to store the input:
    void input(int*);
    Another takes the 2 integer arrays as parameter to add the integers.
    int *add(int *,int*);

    How to divide the input integers into digits?
    If I know how to write void input(int*), then I may know how to write int *add(int *,int*)
    Anyone can help? Any similar example on the net that can inspire me?
    Last edited by Roy01; 12-10-2006 at 08:19 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Could you please be more specific?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you type in
    13579

    I can see that you might store 5 in x[0] representing the length, but what would you store in x[1]?
    Would it be '1', the first digit of input?
    Would it be '9', the least significant value of the input?

    > how to divide the input integer into digits
    It's a lot easier if you read it as a string, expecially if you're considering very long input.

    The rest is just modelling how you would add two numbers on paper
    - start at the right, and add with carry your way to the left.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Quote Originally Posted by Salem
    If you type in
    13579

    I can see that you might store 5 in x[0] representing the length, but what would you store in x[1]?
    Would it be '1', the first digit of input?
    Would it be '9', the least significant value of the input?

    > how to divide the input integer into digits
    It's a lot easier if you read it as a string, expecially if you're considering very long input.

    The rest is just modelling how you would add two numbers on paper
    - start at the right, and add with carry your way to the left.
    Oops,I forgot to mention that, sorry.
    It should be reverse so x[1] should be 9 according to your example.
    If string can't be used, how to divide the integer into digits?
    I asked this question as it is given that the function to store the input:
    void input(int*);
    takes an integer as parameter.
    Last edited by Roy01; 12-10-2006 at 08:17 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you extract digits from an integer using /10 and %10 operations in some kind of loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thanks, I always forget this.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The magic phrase you're looking for is "arbitrary precision arithmetic". It's implemented by the GNU MP library - see

    http://www.swox.com/gmp/

    and if you just want to use it rather than compile code for it, there are a number of implementations including Maxima, an open-source descendent of MACSYMA:

    http://maxima.sourceforge.net/

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thanks, these are quite useful for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 01:55 PM
  3. dynamic array of vectors
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2006, 12:01 AM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM