Division with arrays?

Discussion in 'C Programming' started by Marcin, Feb 1, 2005.

  1. Marcin

    Marcin Guest

    How I can make division of two numbers placed in arrays, example:

    short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};

    short int result[] = a / b; 'division

    without additional lib's and header files, only in standart C.
     
    Marcin, Feb 1, 2005
    #1
    1. Advertising

  2. (Marcin) writes:
    > How I can make division of two numbers placed in arrays, example:
    >
    > short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    > short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >
    > short int result[] = a / b; 'division
    >
    > without additional lib's and header files, only in standart C.


    First, define what you mean by division for arrays. The most obvious
    meaning result would be an array consisting of the result of dividing
    each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
    of different lengths (and the 0's in b would cause problems).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 1, 2005
    #2
    1. Advertising

  3. Marcin

    Kobu Guest

    Marcin wrote:
    > How I can make division of two numbers placed in arrays, example:
    >
    > short int a[] =

    {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    > short int b[] =

    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >
    > short int result[] = a / b; 'division
    >
    > without additional lib's and header files, only in standart C.


    You have to manually do this (go through pairs of elements - one in
    each array). You're arrays are of different sizes, so your result
    array will be the size of the shorter array (and division by zero has
    to be handled).

    example (assuming your declarations for array 'a' and 'b' are done):

    ..double *result;
    ..size_t i;

    ..size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
    sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);

    ..result = malloc(sizeof(*result) * limit);

    /* loop below divides elements of 'a' by 'b' places into result */
    ..for(i = 0; i < limit; i++)
    ..{
    .. if(b[0] = 0)
    .. result[0] = 123456789.123456789; /* handle divide by 0 somehow
    */
    .. else
    .. result[0]= (double)a[0]/b[0];
    ..}

    At this point, the division is done, and you have a dynamically
    allocated buffer (which you can reference like an array) result, which
    holds the division results for each element. You have to handle
    division by zero somehow, possible choosing some value that can't be
    the result of your divisions.

    When you're done with the result array you have to free the buffer
    using:

    free(result);

    Otherwise, you have a memory leak.
     
    Kobu, Feb 1, 2005
    #3
  4. Marcin

    Kobu Guest

    Kobu wrote:
    > Marcin wrote:
    > > How I can make division of two numbers placed in arrays, example:
    > >
    > > short int a[] =

    > {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    > > short int b[] =

    > {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    > >
    > > short int result[] = a / b; 'division
    > >
    > > without additional lib's and header files, only in standart C.

    >
    > You have to manually do this (go through pairs of elements - one in
    > each array). You're arrays are of different sizes, so your result
    > array will be the size of the shorter array (and division by zero has
    > to be handled).
    >
    > example (assuming your declarations for array 'a' and 'b' are done):
    >
    > .double *result;
    > .size_t i;
    >
    > .size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
    > sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);
    >
    > .result = malloc(sizeof(*result) * limit);
    >
    > /* loop below divides elements of 'a' by 'b' places into result */
    > .for(i = 0; i < limit; i++)
    > .{
    > . if(b[0] = 0)
    > . result[0] = 123456789.123456789; /* handle divide by 0

    somehow
    > */
    > . else
    > . result[0]= (double)a[0]/b[0];
    > .}


    all the subscripts within the loop should be i, not 0

    >
    > At this point, the division is done, and you have a dynamically
    > allocated buffer (which you can reference like an array) result,

    which
    > holds the division results for each element. You have to handle
    > division by zero somehow, possible choosing some value that can't be
    > the result of your divisions.
    >
    > When you're done with the result array you have to free the buffer
    > using:
    >
    > free(result);
    >
    > Otherwise, you have a memory leak.
     
    Kobu, Feb 2, 2005
    #4
  5. Marcin

    Luke Wu Guest

    Kobu wrote:
    > Kobu wrote:
    > > Marcin wrote:
    > > > How I can make division of two numbers placed in arrays, example:
    > > >
    > > > short int a[] =

    > > {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    > > > short int b[] =

    > > {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    > > >
    > > > short int result[] = a / b; 'division
    > > >
    > > > without additional lib's and header files, only in standart C.

    > >
    > > You have to manually do this (go through pairs of elements - one in
    > > each array). You're arrays are of different sizes, so your result
    > > array will be the size of the shorter array (and division by zero

    has
    > > to be handled).
    > >
    > > example (assuming your declarations for array 'a' and 'b' are

    done):
    > >
    > > .double *result;
    > > .size_t i;
    > >
    > > .size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
    > > sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);


    the expression to the right of the = operator evaluates to a compile
    time constant, which you can use to statically or automatically
    allocate a result array (if you wanted to)

    > >
    > > .result = malloc(sizeof(*result) * limit);
    > >
    > > /* loop below divides elements of 'a' by 'b' places into result */
    > > .for(i = 0; i < limit; i++)
    > > .{
    > > . if(b[0] = 0)
    > > . result[0] = 123456789.123456789; /* handle divide by 0

    > somehow
    > > */
    > > . else
    > > . result[0]= (double)a[0]/b[0];
    > > .}

    >
    > all the subscripts within the loop should be i, not 0
    >
    > >
    > > At this point, the division is done, and you have a dynamically
    > > allocated buffer (which you can reference like an array) result,

    > which
    > > holds the division results for each element. You have to handle
    > > division by zero somehow, possible choosing some value that can't

    be
    > > the result of your divisions.
    > >
    > > When you're done with the result array you have to free the buffer
    > > using:
    > >
    > > free(result);
    > >
    > > Otherwise, you have a memory leak.
     
    Luke Wu, Feb 2, 2005
    #5
  6. On Tue, 01 Feb 2005 22:12:21 +0000, Keith Thompson wrote:

    > (Marcin) writes:
    >> How I can make division of two numbers placed in arrays, example:
    >>
    >> short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    >> short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >>
    >> short int result[] = a / b; 'division
    >>
    >> without additional lib's and header files, only in standart C.

    >
    > First, define what you mean by division for arrays. The most obvious
    > meaning result would be an array consisting of the result of dividing
    > each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
    > of different lengths (and the 0's in b would cause problems).


    The question did state that theree are 2 numbers, so presumable each array
    a and b represents a number in some fashion. We don't know how but at a
    guess it is probably as a sequence of decimal digits. In which case some
    form of decimal long division looks appropriate.

    Lawrence
     
    Lawrence Kirby, Feb 2, 2005
    #6
  7. On Tue, 01 Feb 2005 15:52:06 -0800, Kobu wrote:

    >
    > Marcin wrote:
    >> How I can make division of two numbers placed in arrays, example:
    >>
    >> short int a[] =

    > {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    >> short int b[] =

    > {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >>
    >> short int result[] = a / b; 'division
    >>
    >> without additional lib's and header files, only in standart C.

    >
    > You have to manually do this (go through pairs of elements - one in
    > each array). You're arrays are of different sizes, so your result
    > array will be the size of the shorter array (and division by zero has
    > to be handled).


    1000 / 1 is 1000, i.e. the result is not the size of the "shorter array".
    If there are no leading zeros in the numbers it would be
    nelems(a)-nelems(b)+1, and if b is larger the result is trivially zero.

    > example (assuming your declarations for array 'a' and 'b' are done):
    >
    > .double *result;
    > .size_t i;
    >
    > .size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
    > sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);


    Here you are using the number of elements of the larger of the 2 arrays
    which would work but isn't optimal. You might just as well use simply the
    number of elements of a, the result of a valid integer division can't be
    larger than the dividend.

    Lawrence
     
    Lawrence Kirby, Feb 2, 2005
    #7
  8. Marcin

    Kobu Guest

    Lawrence Kirby wrote:
    > On Tue, 01 Feb 2005 15:52:06 -0800, Kobu wrote:
    >
    > >
    > > Marcin wrote:
    > >> How I can make division of two numbers placed in arrays, example:
    > >>
    > >> short int a[] =

    > > {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    > >> short int b[] =

    > > {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    > >>
    > >> short int result[] = a / b; 'division
    > >>
    > >> without additional lib's and header files, only in standart C.

    > >
    > > You have to manually do this (go through pairs of elements - one in
    > > each array). You're arrays are of different sizes, so your result
    > > array will be the size of the shorter array (and division by zero

    has
    > > to be handled).

    >
    > 1000 / 1 is 1000, i.e. the result is not the size of the "shorter

    array".
    > If there are no leading zeros in the numbers it would be
    > nelems(a)-nelems(b)+1, and if b is larger the result is trivially

    zero.

    by size I mean: sizeof(array)/sizeof(*array)
    I think I misunderstood the question, thinking that each array held a
    separate number in each position. I now see what he probably meant.

    >
    > > example (assuming your declarations for array 'a' and 'b' are

    done):
    > >
    > > .double *result;
    > > .size_t i;
    > >
    > > .size_t limit = (sizeof(a)/sizeof(*a) > sizeof(b)/sizeof(*b)) :
    > > sizeof(a)/sizeof(*a) ? sizeof(b)/sizeof(*b);


    should be < not > (under my original assumption)

    >
    > Here you are using the number of elements of the larger of the 2

    arrays
    > which would work but isn't optimal. You might just as well use simply

    the
    > number of elements of a, the result of a valid integer division can't

    be
    > larger than the dividend.


    yeap, good point

    >
    > Lawrence
     
    Kobu, Feb 2, 2005
    #8
  9. On 1 Feb 2005 13:57:28 -0800, (Marcin) wrote:


    >How I can make division of two numbers placed in arrays, example:
    >
    >short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    >short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >
    >short int result[] = a / b; 'division


    Is b's value 10000000000000000000000002 or 20000000000000000000000001 ?
    If it's the latter, the problem is very simple. If not, it's very
    slightly more complicated.


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
     
    Kevin D. Quitt, Feb 2, 2005
    #9
  10. Lawrence Kirby <> writes:
    > On Tue, 01 Feb 2005 22:12:21 +0000, Keith Thompson wrote:
    >
    >> (Marcin) writes:
    >>> How I can make division of two numbers placed in arrays, example:
    >>>
    >>> short int a[] = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
    >>> short int b[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
    >>>
    >>> short int result[] = a / b; 'division
    >>>
    >>> without additional lib's and header files, only in standart C.

    >>
    >> First, define what you mean by division for arrays. The most obvious
    >> meaning result would be an array consisting of the result of dividing
    >> each element in turn (a[0]/b[0], a[1]/b[1], ...), but your arrays are
    >> of different lengths (and the 0's in b would cause problems).

    >
    > The question did state that theree are 2 numbers, so presumable each array
    > a and b represents a number in some fashion. We don't know how but at a
    > guess it is probably as a sequence of decimal digits. In which case some
    > form of decimal long division looks appropriate.


    Ok, that's probably what the OP meant, but he needs to clarify it. Do
    the arrays represent sequences of decimal digits? In what order? How
    are negative values represented (are only non-negative values
    representable).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 2, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,891
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,339
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    588
  4. Replies:
    94
    Views:
    4,662
    ┬Ča\\/b
    Feb 9, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,181
    Philipp
    Jan 20, 2009
Loading...

Share This Page