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

 
 
John Harrison
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.
>
> <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[i] = 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[i] == 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[i]) && !in_array(a, i - 1, a[i]))
{
cout << a[i] << ' ';
}
}
cout << '\n';
}

Untested code.

john


 
Reply With Quote
 
 
 
 
Charley
Guest
Posts: n/a
 
      07-14-2003
"John Harrison" <(E-Mail Removed)> wrote in message news:<bervbm$813eb$(E-Mail Removed)-berlin.de>...
> "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.
> >
> > <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[i] = 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[i] == 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[i]) && !in_array(a, i - 1, a[i]))
> {
> cout << a[i] << ' ';
> }
> }
> 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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-15-2003

"Charley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John Harrison" <(E-Mail Removed)> wrote in message

news:<bervbm$813eb$(E-Mail Removed)-berlin.de>...
> > "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.
> > >
> > > <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[i] = 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[i] == 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[i]) && !in_array(a, i - 1, a[i]))
> > {
> > cout << a[i] << ' ';
> > }
> > }
> > 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[i]) && !in_array(a, i - 1, a[i]))

The second call to in_array looks for a[i] 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[i]) && !in_array(a + (i + 1), a_size - (i
+ 1), a[i]))

and it would still work (I think). Here instead of looking for a[i] in a[0]
... a[i - 1], we are looking for a[i] 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


 
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 Thomas Matthews C++ 0 07-13-2003 06:13 PM
Re: array copying to another array MiniDisc_2k2 C++ 1 07-13-2003 02:35 PM



Advertisments