Re: array copying to another array

Discussion in 'C++' started by John Harrison, Jul 13, 2003.

  1. "Charley" <> wrote in message
    news:...
    > 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.
    >
    > <code>
    > void Intersection (int arrayA[6], int arrayB[6])
    > {
    > int Ai(0), Bj(0), counter(0);
    > cout<<"The intersection of ArayA and ArrayB is: "<<endl;
    > cout<<"{";
    > for (Ai=0; Ai<6; Ai++)
    > {
    > for (Bj=0; Bj<6; Bj++)
    > {
    > if (arrayA[Ai] == arrayB[Bj])
    > {
    > counter++;
    > }
    > }
    > if (counter != 0)
    > {
    > cout<<arrayA[Ai];
    > }
    > counter=0;
    > }
    > cout<<"}";
    >
    > }
    > </code>


    Its strange because I don't see any attempt to copy anything in the code you
    posted. Of course writing a = b[j] will copy the jth element of b to the
    ith element of a, what else would it do? Also I don't see the need to copy
    anything, in order to print an array intersection. Perhaps you should say
    what you are trying to do, rather than what you are having trouble with. It
    could be that you are just going about things the wrong way.

    Nevertheless here some code that print the intersection of two arrays,
    without repeated elements.

    // returns true if b is in array a[0 .. n-1]
    bool in_array(const int* a, int n, int b)
    {
    for (int i = 0; i < n; ++i)
    if (a == b)
    return true;
    return false;
    }

    // print the intersection of a[0 .. a_size-1] and b[0 .. b_size-1] without
    repeated elements
    int print_array_intersect(const int a*, int a_size, const int* b, int
    b_size)
    {
    for (int i = 0; i < a_size; ++i)
    {
    if (in_array(b, b_size, a) && !in_array(a, i - 1, a))
    {
    cout << a << ' ';
    }
    }
    cout << '\n';
    }

    Untested code.

    john
     
    John Harrison, Jul 13, 2003
    #1
    1. Advertising

  2. John Harrison

    Charley Guest

    "John Harrison" <> wrote in message news:<bervbm$813eb$-berlin.de>...
    > "Charley" <> wrote in message
    > news:...
    > > 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.
    > >
    > > <code>
    > > void Intersection (int arrayA[6], int arrayB[6])
    > > {
    > > int Ai(0), Bj(0), counter(0);
    > > cout<<"The intersection of ArayA and ArrayB is: "<<endl;
    > > cout<<"{";
    > > for (Ai=0; Ai<6; Ai++)
    > > {
    > > for (Bj=0; Bj<6; Bj++)
    > > {
    > > if (arrayA[Ai] == arrayB[Bj])
    > > {
    > > counter++;
    > > }
    > > }
    > > if (counter != 0)
    > > {
    > > cout<<arrayA[Ai];
    > > }

    > counter=0;
    > > }
    > > cout<<"}";
    > >
    > > }
    > > </code>

    >
    > Its strange because I don't see any attempt to copy anything in the code you
    > posted. Of course writing a = b[j] will copy the jth element of b to the
    > ith element of a, what else would it do? Also I don't see the need to copy
    > anything, in order to print an array intersection. Perhaps you should say
    > what you are trying to do, rather than what you are having trouble with. It
    > could be that you are just going about things the wrong way.
    >
    > Nevertheless here some code that print the intersection of two arrays,
    > without repeated elements.
    >
    > // returns true if b is in array a[0 .. n-1]
    > bool in_array(const int* a, int n, int b)
    > {
    > for (int i = 0; i < n; ++i)
    > if (a == b)
    > return true;
    > return false;
    > }
    >
    > // print the intersection of a[0 .. a_size-1] and b[0 .. b_size-1] without
    > repeated elements
    > int print_array_intersect(const int a*, int a_size, const int* b, int
    > b_size)
    > {
    > for (int i = 0; i < a_size; ++i)
    > {
    > if (in_array(b, b_size, a) && !in_array(a, i - 1, a))
    > {
    > cout << a << ' ';
    > }
    > }
    > cout << '\n';
    > }
    >
    > Untested code.
    >
    > john

    *****************************************************************************
    John

    Thanks for the tips. I tried to use your code but am not in full
    understanding on what it does. I have never seen some of the syntax
    used in the sample you sent. I would most appreciate comments on what
    is going on.

    Thanks again
    thechasboi
     
    Charley, Jul 15, 2003
    #2
    1. Advertising

  3. "Charley" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<bervbm$813eb$-berlin.de>...
    > > "Charley" <> wrote in message
    > > news:...
    > > > 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.
    > > >
    > > > <code>
    > > > void Intersection (int arrayA[6], int arrayB[6])
    > > > {
    > > > int Ai(0), Bj(0), counter(0);
    > > > cout<<"The intersection of ArayA and ArrayB is: "<<endl;
    > > > cout<<"{";
    > > > for (Ai=0; Ai<6; Ai++)
    > > > {
    > > > for (Bj=0; Bj<6; Bj++)
    > > > {
    > > > if (arrayA[Ai] == arrayB[Bj])
    > > > {
    > > > counter++;
    > > > }
    > > > }
    > > > if (counter != 0)
    > > > {
    > > > cout<<arrayA[Ai];
    > > > }

    > > counter=0;
    > > > }
    > > > cout<<"}";
    > > >
    > > > }
    > > > </code>

    > >
    > > Its strange because I don't see any attempt to copy anything in the code

    you
    > > posted. Of course writing a = b[j] will copy the jth element of b to

    the
    > > ith element of a, what else would it do? Also I don't see the need to

    copy
    > > anything, in order to print an array intersection. Perhaps you should

    say
    > > what you are trying to do, rather than what you are having trouble with.

    It
    > > could be that you are just going about things the wrong way.
    > >
    > > Nevertheless here some code that print the intersection of two arrays,
    > > without repeated elements.
    > >
    > > // returns true if b is in array a[0 .. n-1]
    > > bool in_array(const int* a, int n, int b)
    > > {
    > > for (int i = 0; i < n; ++i)
    > > if (a == b)
    > > return true;
    > > return false;
    > > }
    > >
    > > // print the intersection of a[0 .. a_size-1] and b[0 .. b_size-1]

    without
    > > repeated elements
    > > int print_array_intersect(const int a*, int a_size, const int* b, int
    > > b_size)
    > > {
    > > for (int i = 0; i < a_size; ++i)
    > > {
    > > if (in_array(b, b_size, a) && !in_array(a, i - 1, a))
    > > {
    > > cout << a << ' ';
    > > }
    > > }
    > > cout << '\n';
    > > }
    > >
    > > Untested code.
    > >
    > > john

    >

    ****************************************************************************
    *
    > John
    >
    > Thanks for the tips. I tried to use your code but am not in full
    > understanding on what it does.


    Did it work?

    > I have never seen some of the syntax
    > used in the sample you sent. I would most appreciate comments on what
    > is going on.
    >
    > Thanks again
    > thechasboi


    I don't think I did anything unusual syntax wise, which bit don't you
    understand?

    The only bit I thought perhaps would be new to you was the treatment of
    arrays as pointers. This is very common, very useful and well worth knowing.

    You wrote your function like this

    void Intersection (int arrayA[6], int arrayB[6])

    This is legal, but it also is deceptive. It seems to imply that you must
    pass an array of size 6 to your function, but this is not true. The function
    Intersection would accept an int array of *any* size, try it. Of course the
    code itself assumes that the array is size 6 so it wouldn't work if you
    passed it a different size array, but it would compile.

    A slightly better syntax is this

    void Intersection (int arrayA[], int arrayB[])

    which emphasises that Intersection will accept any size of array.

    But the truth is this

    void Intersection (int* arrayA, int* arrayB)

    Intersection accepts two pointers. Both of the proceding ways of writing
    Intersection are actually converted by the compiler into the pointer
    version, which is the true representation of what is going on.

    Pointers and arrays are nearly equivalent in C++, arrays convert to
    pointers, this is what happens when you call Intersection with an array, the
    array is converted to a pointer to its first element. Also pointers can be
    accessed using the array [] syntax. So in Intersection when you write

    if (arrayA[Ai] == arrayB[Bj])

    arrayA and arrayB are actually pointers but you can access the integers
    pointed to using [].

    So because function can accept arrays of any size by treating them as
    pointers, it usually a good idea to pass the size of the array in as a
    parameter as well, that's what I'm doing here

    // returns true if b is in array a[0 .. n-1]
    bool in_array(const int* a, int n, int b)

    a is a pointer, which originally was an array of size n. These means
    in_array can work on an array of any size, which is obviously useful. In
    particular in this code

    if (in_array(b, b_size, a) && !in_array(a, i - 1, a))

    The second call to in_array looks for a in the first i -1 elements of a.
    This shows the flexibility that using pointers and sizes gives you. Any
    portion of an array can be treated as if was an array in it own right. For
    instance the above code could have been written

    if (in_array(b, b_size, a) && !in_array(a + (i + 1), a_size - (i
    + 1), a))

    and it would still work (I think). Here instead of looking for a in a[0]
    ... a[i - 1], we are looking for a in a[i + 1] .. a[a_size - 1]. In both
    cases the effect is the same, we skip duplicates values in a.

    I suppose the other new piece of syntax is the use of const. In in_array
    above 'const int* a' says that in_array will not modify the values being
    pointed to by a. In other words in_array uses the values of a but does not
    change them. In this case const just helps you understand what in_array
    does, the program would work perfectly well if you removed the const from
    in_array and print_array_intersect, but there are cases in C++ where code
    won't work without const, so its a good idea to get into the habit of using
    const as soon as possible.

    john
     
    John Harrison, Jul 15, 2003
    #3
    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. Thomas Matthews

    Re: array copying to another array

    Thomas Matthews, Jul 13, 2003, in forum: C++
    Replies:
    0
    Views:
    389
    Thomas Matthews
    Jul 13, 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