473,466 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparing 2 unequal arrays...

Hey, for c++ does any body have a way to compare two arrays? Say one
has the numbers 1-5000, the other has *most* of the numbers from
1-5000, is there a way to display the numbers that are in the 1st
array, but not in the 2nd array? Or load those numbers in a 3rd array?

Thats the simple version...there are a couple other problems: ex:
the 10th value of the 2nd array corresponds to the 12th value of the
first.
- you cant simply make a for loop and compare each individual one...

The arrays are different sizes...one having 5000 values, the other
like 4700ish.

Any ideas? I was thinking of something like writing the smaller array
into a file, and then having something read the values of the first
array, and if it doesnt find it in the file, then it sends it to a
diff file or something...but I have NO IDEA how to code that...

Any ideas/thoughts/etc would be very welcome =)
Jul 19 '05 #1
6 10550

"Raistlin" <Ic**********@hotmail.com> wrote in message
news:c7**************************@posting.google.c om...
Hey, for c++ does any body have a way to compare two arrays? Say one
has the numbers 1-5000, the other has *most* of the numbers from
1-5000, is there a way to display the numbers that are in the 1st
array, but not in the 2nd array? Or load those numbers in a 3rd array?

Thats the simple version...there are a couple other problems: ex:
the 10th value of the 2nd array corresponds to the 12th value of the
first.
- you cant simply make a for loop and compare each individual one...

The arrays are different sizes...one having 5000 values, the other
like 4700ish.

Any ideas? I was thinking of something like writing the smaller array
into a file, and then having something read the values of the first
array, and if it doesnt find it in the file, then it sends it to a
diff file or something...but I have NO IDEA how to code that...

Any ideas/thoughts/etc would be very welcome =)


Play with the following:

---- main.cpp ----
#include <iostream>
#include <map>

int main()
{
using namespace std;
int a[] = { 1, 3, 5, 600, 600, 123, 145 };
int b[] = { 3, 3, 1, 600, 5000, 145 };

map<int, pair<bool, bool> > m;

for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
pair<bool, bool>& p = m[a[i]];
p.first = true;
}

for (int i = 0; i < sizeof(b) / sizeof(b[0]); ++i)
{
pair<bool, bool>& p = m[b[i]];
p.second = true;
}

for (map<int, pair<bool, bool> >::const_iterator cit = m.begin(); cit !=
m.end(); ++cit)
{
int key = cit->first;
pair<bool, bool> const& p = cit->second;
cout << key << " " << p.first << " " << p.second << endl;
}

return 0;
}

/* Output:

1 1 1
3 1 1
5 1 0
123 1 0
145 1 1
600 1 1
5000 0 1

thus:
1 is in a and b
3 is in a and b
5 is in a not b
....
5000 is not in a but in b
etc etc
*/
--------------------

h.t.h
Conrad Weyns
Jul 19 '05 #2
Well, this is not inherently C++, it's more a general algorithm question.
I add another way over that suggested by Conrad: sort the two arrays (I
know qsort C method, but surely there's something equivalent in C++
standard libraries), and then you can simply run with two pointers over
the two arrays: when the value pointed by one is greater that the
other, increase by one the other (pointer math...) - whenever the two
pointers swap role (the one which before pointed a number greater now
points a number lesser than the other) it means that the value given by
the pointer which has remained fixed isn't present in the other array;
if they are the same, you've found an exact match, you can print it, and
then increase both.

This is very C-like, but it should work.

Time needed:
sorting (n lg n) + comparisons (n) = n lg n. I think it's the best you can
sort out, if the arrays are unordered.

--
/**
* Mattia Belletti - Undergraduate student @ cs.unibo.it
* ICQ: 33292311 - email: mb******@cs.unibo.it
* IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
* Linux registered user 299762 @ Linux registered machine 213003
*/

Jul 19 '05 #3
"Raistlin" <Ic**********@hotmail.com> wrote in message
news:c7**************************@posting.google.c om...
Hey, for c++ does any body have a way to compare two arrays? Say one
has the numbers 1-5000, the other has *most* of the numbers from
1-5000, is there a way to display the numbers that are in the 1st
array, but not in the 2nd array? Or load those numbers in a 3rd array?

Thats the simple version...there are a couple other problems: ex:
the 10th value of the 2nd array corresponds to the 12th value of the
first.
- you cant simply make a for loop and compare each individual one...

The arrays are different sizes...one having 5000 values, the other
like 4700ish.

Any ideas? I was thinking of something like writing the smaller array
into a file, and then having something read the values of the first
array, and if it doesnt find it in the file, then it sends it to a
diff file or something...but I have NO IDEA how to code that...


Sorting both sequences first is the most efficient approach.

The standard C++ algorithm std::set_difference outputs the
'difference' of two sorted sequences (that is, items of the first
one that are not present in the second one).

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

typedef std::vector<int> Items;

void printDiff(Items& a, Items& b)
{
std::sort( a.begin(), a.end() );
std::sort( b.begin(), b.end() );
std::set_difference
( a.begin(), a.end(), b.begin(), b.end()
, std::ostream_iterator<int>(std::cout,"\n") );
}

Instead of printing the output to cout using ostream_iterator,
you could as well store it into a new collection:
Items diff;
std::set_difference
( a.begin(), a.end(), b.begin(), b.end()
, std::back_inserter(diff) );
// diff now contains the requested items.
I hope this helps,
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++

Jul 19 '05 #4

"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote in message
news:3f******@news.swissonline.ch...
"Raistlin" <Ic**********@hotmail.com> wrote in message
news:c7**************************@posting.google.c om...
Hey, for c++ does any body have a way to compare two arrays? Say one
has the numbers 1-5000, the other has *most* of the numbers from
1-5000, is there a way to display the numbers that are in the 1st
array, but not in the 2nd array? Or load those numbers in a 3rd array?

Thats the simple version...there are a couple other problems: ex:
the 10th value of the 2nd array corresponds to the 12th value of the
first.
- you cant simply make a for loop and compare each individual one...

The arrays are different sizes...one having 5000 values, the other
like 4700ish.

Any ideas? I was thinking of something like writing the smaller array
into a file, and then having something read the values of the first
array, and if it doesnt find it in the file, then it sends it to a
diff file or something...but I have NO IDEA how to code that...
Sorting both sequences first is the most efficient approach.

The standard C++ algorithm std::set_difference outputs the
'difference' of two sorted sequences


The key here is "difference".
(that is, items of the first one that are not present in the second one).
I think this is only true if there are no duplicates in a collection else
you'll get the number of times one specific value occurs *more* often than
in the other sequence. Ehm, I should be able to phrase that better, so:

a[] = { 7, 7, 7 };
b[] = { 7 };

difference = { 7, 7 };

I think anyway..
Regards,
Conrad Weyns


#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

typedef std::vector<int> Items;

void printDiff(Items& a, Items& b)
{
std::sort( a.begin(), a.end() );
std::sort( b.begin(), b.end() );
std::set_difference
( a.begin(), a.end(), b.begin(), b.end()
, std::ostream_iterator<int>(std::cout,"\n") );
}

Instead of printing the output to cout using ostream_iterator,
you could as well store it into a new collection:
Items diff;
std::set_difference
( a.begin(), a.end(), b.begin(), b.end()
, std::back_inserter(diff) );
// diff now contains the requested items.
I hope this helps,
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++

Jul 19 '05 #5

"Conrad Weyns" <we***********@invalid.online.no> wrote in message
news:bl***********@news.dataguard.no...

"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote in message
news:3f******@news.swissonline.ch...
"Raistlin" <Ic**********@hotmail.com> wrote in message
news:c7**************************@posting.google.c om... []
Sorting both sequences first is the most efficient approach.

The standard C++ algorithm std::set_difference outputs the
'difference' of two sorted sequences
The key here is "difference".
(that is, items of the first one that are not present in the second one).
Sorry! I see it now, that's exactly what you meant....
conrad weyns


I think this is only true if there are no duplicates in a collection else
you'll get the number of times one specific value occurs *more* often than
in the other sequence. Ehm, I should be able to phrase that better, so:

a[] = { 7, 7, 7 };
b[] = { 7 };

difference = { 7, 7 };

I think anyway..
Regards,
Conrad Weyns

[]
I hope this helps,
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++


Jul 19 '05 #6

"Raistlin" <Ic**********@hotmail.com> wrote in message news:c7**************************@posting.google.c om...
Hey, for c++ does any body have a way to compare two arrays?
Well the equality operator doesn't really work on arrays at all (due to the misimplementation
of arrays in the language). There are various algorithms for doing element-wise comparisons.

See the Equal library function for simple equality tests.
Say one
has the numbers 1-5000, the other has *most* of the numbers from
1-5000, is there a way to display the numbers that are in the 1st
array, but not in the 2nd array? Or load those numbers in a 3rd array?
Are the arrays sorted? It makes a difference as to the algorithm.

Thats the simple version...there are a couple other problems: ex:
the 10th value of the 2nd array corresponds to the 12th value of the
first.
- you cant simply make a for loop and compare each individual one...


Is the order of the elements found important? Are the numbers unique?
There are solutions, but we need a more conscise description of what you want.
Jul 19 '05 #7

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
4
by: eoghan.kenny | last post by:
Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
11
by: Sheldon | last post by:
Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...

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.