Re: array copying to another array

Discussion in 'C++' started by Thomas Matthews, Jul 13, 2003.

  1. Charley wrote:
    > I have a problem. I do not understand how to copy selected elements
    > from one array to another. I have tried x=y, pointers, I even
    > tried replace=x and y=replace. This gets me no where. I am
    > creating a simple program that will give me the intersection of the
    > two arrays with out the repeated numbers. I have so it gives me the
    > intersection but not get rid of the repeated numbers.
    >


    class Null_Array_Pointer_Exception
    {
    };

    void
    Intersection(const int * a, const int * b, unsigned int size,
    int * result, unsigned int& num_common_items)
    {
    if ((a == NULL) || (b == NULL))
    throw Null_Array_Pointer_Exception();
    if (result == NULL)
    result = new int [size];
    num_common_items = 0;
    for (unsigned int a_index = 0; a_index < size; ++a_index)
    {
    for (unsigned int b_index = 0; b_index < size; ++b_index)
    {
    if (a[a_index] == b[b_index])
    {
    result[num_common_items++] = a[a_index];
    break; // don't search the rest of array b.
    }
    }
    }
    return;
    }

    The above function fills the result array with members of
    A that are contained in B. If A exists more that once
    in B, only one instance is copied to the result.

    There is check for any duplicates within A.

    The function signature shows that both A and B must be
    arrays of the same size and type. The size parameter
    is specified because C++ has no facilities for determining
    the size of an array from an array variable. The pointers
    of A and B are declared as pointers to constant data
    since this function does not change the contents of
    either A or B.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Jul 13, 2003
    #1
    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. MiniDisc_2k2

    Re: array copying to another array

    MiniDisc_2k2, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    364
    Philip Lawatsch
    Jul 13, 2003
  2. John Harrison

    Re: array copying to another array

    John Harrison, Jul 13, 2003, in forum: C++
    Replies:
    2
    Views:
    415
    John Harrison
    Jul 15, 2003
  3. johnny

    copying a 2-dimension array into another

    johnny, Jun 18, 2006, in forum: C Programming
    Replies:
    1
    Views:
    299
  4. DaTurk
    Replies:
    2
    Views:
    391
    Jim Langston
    Sep 11, 2007
  5. janus

    Copying one array to another array

    janus, Feb 3, 2010, in forum: C Programming
    Replies:
    16
    Views:
    687
    Peter Nilsson
    Feb 3, 2010
Loading...

Share This Page