473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting arrays

How can I extract common elements of two number arrays into another
array?

// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}

Jun 15 '07 #1
7 1833
In article <11**********************@i13g2000prf.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
>How can I extract common elements of two number arrays into another
array?

// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}
What doesn't work about it?

You need to show us a complete program.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 15 '07 #2
"Umesh" writes:
How can I extract common elements of two number arrays into another
array?

// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}
Congratulations Umesh! You finally figured out that the question belongs in
the message. The code fragment you posted doesn't show any means of starting
m at 0. Does the real code take care of that?
Jun 15 '07 #3
On 15 Jun, 13:55, Umesh <fraternitydispo...@gmail.comwrote:
How can I extract common elements of two number arrays into another
array?

// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}
This code looks OK to me, as long as you set m to zero beforehand and
r is big enough. The one snag I can see is that, if an element occurs
more than once in a list, and at least once in the other, it may be
selected in r more times than you want. One way round that might be to
change the value in p to some sort of non-value (eg you could use 0,
or -1, if this wasn't going to occur in the real data). An alternative
might be to have another array to indicate whether the value in p had
been "used" yet or not. It's up to you.

Hope this helps.
Paul.

Jun 15 '07 #4
gw****@aol.com writes:
On 15 Jun, 13:55, Umesh <fraternitydispo...@gmail.comwrote:
>How can I extract common elements of two number arrays into another
array?

// the following doesn't work
for(k=0;k<10;k++)
for(l=0;l<15;l++)
if(p[k]==q[l]) {r[m]=p[k];++m;}

This code looks OK to me, as long as you set m to zero beforehand and
r is big enough. The one snag I can see is that, if an element occurs
more than once in a list, and at least once in the other, it may be
selected in r more times than you want. One way round that might be to
change the value in p to some sort of non-value (eg you could use 0,
or -1, if this wasn't going to occur in the real data). An alternative
might be to have another array to indicate whether the value in p had
been "used" yet or not. It's up to you.
That "other array" already exists (r). If I were forced to use this
rather inefficient method, I'd write a "membership" function:

int in(int x, int p[], int l)
{
/* Return 1 if x is equal to any element in p[0]..p[l-1]; 0 otherwise */
int k;
for (k = 0; k < l; k++)
if (x == p[k])
return 1;
return 0;
}

and then do something like:

for (k = 0; k < sz1; k++)
if (!in(p[k], r, m) && in(p[k], q, s2))
r[m++] = p[k];

A much better way, is to sort p and q and the do a sort of
"anti-merge". That will be much faster for large arrays.

--
Ben.
Jun 15 '07 #5
Count no. of same elements in an number array and then display the
result.

Jun 16 '07 #6
On Jun 16, 7:34 pm, Umesh <fraternitydispo...@gmail.comwrote:
Count no. of same elements in an number array and then display the
result.
using this code if same number is there in any of the array more than
once,
it will copy again. Is it really needed?

Jun 16 '07 #7
Umesh wrote:
Count no. of same elements in an number array and then display the
result.
I get `17`.

--
Chris "DYOH" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2624
by: Nazgul | last post by:
Hi! I want to implement a small tool in Python for distributing "patches" and I need Your advice. This application should be able to package all files chosen by a user into a self-extracting.exe...
2
2518
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
4
3860
by: effendi | last post by:
How can I extract differences between two arrays using Javascript? I hv for example, apple, orange, pear and another array with apple, orange and I would like to get pear. Thanks.
36
5260
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
0
1474
by: k_nil | last post by:
I have a link on my web page for a self extracting executable file placed on the server. When the link is clicked, 1) i could see dialog box with open or save options 2) when open clicked, self...
2
2191
by: bjm | last post by:
I created a self extracting zip file with about 9000 files in it. I extracted it manually from the command line without a problem. However, when I tried to do the same extraction at the same...
1
1924
by: rpjd | last post by:
I am completely new to this so please bear with me here. My project involves a webpage executing php scripts via an xmlhttprequest which queries a database and returns data to the webpage. This code...
6
4427
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
5
3627
by: Mukesh | last post by:
Hi, I am using framework 2.0. I am writing a foreach loop that will extract single dimensional arrays out of double dimensional array. I am trying writing something like this. string ...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.