Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: array copying to another array

Reply
Thread Tools

Re: array copying to another array

 
 
MiniDisc_2k2
Guest
Posts: n/a
 
      07-13-2003

"Charley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have a problem. I do not understand how to copy selected elements
> from one array to another. I have tried x[i]=y[i], pointers, I even
> tried replace=x[i] and y[i]=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


 
Reply With Quote
 
 
 
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-13-2003


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-

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying one array to another array janus C Programming 16 02-03-2010 09:20 PM
Copying array to an array, I really thought I knew what was going on. DaTurk C++ 2 09-11-2007 12:04 AM
copying a 2-dimension array into another johnny C Programming 1 06-18-2006 02:50 PM
Re: array copying to another array John Harrison C++ 2 07-15-2003 06:22 AM
Re: array copying to another array Thomas Matthews C++ 0 07-13-2003 06:13 PM



Advertisments