Re: array copying to another array

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

  1. MiniDisc_2k2

    MiniDisc_2k2 Guest

    "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.


    I'm not sure what you mean by "repeated numbers," could you clarify?

    [snip]

    > 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;


    What's up with this block of code? Why don't you just do

    if (arrayA[Ai] == arrayB[Bj])
    cout << arrayA[Ai];

    > }
    > cout<<"}";
    >
    > }


    [snip]

    I'm not quite sure why you would need to copy elements, but here's how you
    do it with int arrays(example):

    arrayA[3] = arrayB[4];

    This will work with any types that have an = operator. More generically, you
    could do:

    bool atob; // copy from arrayA to arrayB?
    int arrayAindex, arrayBindex;
    // fill those three variables
    atob?arrayB[arrayBindex]:arrayA[arrayAindex] =
    atob?arrayA[arrayAindex]:arrayB[arrayBindex];

    In case you didn't understand the ?: operator, that's the same as saying

    if (atob)
    arrayB[arrayBindex] = arrayA[arrayAindex];
    else
    arrayA[arrayAindex] = arrayB[arrayBindex];


    --
    MiniDisc_2k2
    MiniDisc_2k2, Jul 13, 2003
    #1
    1. Advertising

  2. MiniDisc_2k2 wrote:

    >
    >>if (arrayA[Ai] == arrayB[Bj])
    >>{
    >>counter++;
    >>}
    >>}
    >>if (counter != 0)
    >>{
    >>cout<<arrayA[Ai];
    >>}
    >>counter=0;

    >
    >
    > What's up with this block of code? Why don't you just do
    >
    > if (arrayA[Ai] == arrayB[Bj])
    > cout << arrayA[Ai];
    >


    Well, this would just not be the same.
    It could print arrayA[Ai] more than once if arrayB has several
    occourances of arrayA[Ai] in it ..

    -ph-
    Philip Lawatsch, Jul 13, 2003
    #2
    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. John Harrison

    Re: array copying to another array

    John Harrison, Jul 13, 2003, in forum: C++
    Replies:
    2
    Views:
    398
    John Harrison
    Jul 15, 2003
  2. Thomas Matthews

    Re: array copying to another array

    Thomas Matthews, Jul 13, 2003, in forum: C++
    Replies:
    0
    Views:
    371
    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:
    287
  4. DaTurk
    Replies:
    2
    Views:
    369
    Jim Langston
    Sep 11, 2007
  5. janus

    Copying one array to another array

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

Share This Page