473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Cartesian product

I have a program that creates two sets, one thru user interaction and
the other with the use of an array. Can anyone help with coding for
finding the cartesian product of the two sets; i.e a relation?

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{

int arr[] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29};
int arrSize = sizeof(arr) / sizeof(int);
set<intA;
set<intB(arr,arr+arrSize);

int num1=0;
int num2=0;

for(int x=0;x<20;x++)
{
cout << "Please enter an even integer less than 100:" << endl;
cin >num1;
if(A.find(num1) != A.end())

cout << num1 << " has already been entered!"
<< endl;

else if((num1 % 2) != 0)
cout << "That is not an even number!" << endl;
else
A.insert(num1);
}

cout << endl;

cout << "Set A has " << A.size() << "elements." <<endl;
cout << "Set B has " << B.size() << "elements." << endl;
system("pause");

return 0;


}

I have included the code below I got from a site that suggests
creating a struct pair and defining an operator, but I do not
understand it.

struct Pair{
int i;
int j;
};
int operator==(const Pair& p1,const Pair& p2){
return p1.i==p2.i && p1.j==p2.j;
}
set<pairresult;

Feb 11 '07 #1
2 7292
zf*****@umd.umich.edu wrote:
I have a program that creates two sets, one thru user interaction and
the other with the use of an array. Can anyone help with coding for
finding the cartesian product of the two sets; i.e a relation?

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{

int arr[] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29};
int arrSize = sizeof(arr) / sizeof(int);
set<intA;
set<intB(arr,arr+arrSize);

int num1=0;
int num2=0;

for(int x=0;x<20;x++)
{
cout << "Please enter an even integer less than 100:" << endl;
cin >num1;
if(A.find(num1) != A.end())

cout << num1 << " has already been entered!"
<< endl;

else if((num1 % 2) != 0)
cout << "That is not an even number!" << endl;
else
A.insert(num1);
}

cout << endl;

cout << "Set A has " << A.size() << "elements." <<endl;
cout << "Set B has " << B.size() << "elements." << endl;
system("pause");

return 0;


}

I have included the code below I got from a site that suggests
creating a struct pair and defining an operator, but I do not
understand it.

struct Pair{
int i;
int j;
};
int operator==(const Pair& p1,const Pair& p2){
return p1.i==p2.i && p1.j==p2.j;
}
set<pairresult;
I'm not sure about that code either.

A catesian product is a set of pairs. Your sets are sets of integers so
your cartesian product is a set of pairs of integers. So you do need
something like this

struct Pair
{
int x;
int y;
};

set<Paircartesian_product;

But, it is a rule of the C++ set class that any element that you want to
be a member of a set must have the operator< defined (a better name for
set in C++ would be ordered_set, because its an ordered set you need to
define operator<).

So you must write

bool operator<(const Pair& p1, const Pair& p2)
{
...
}

That's what is wrong with the code you posted it defines operator== not
operator<. It doesn't matter how you define operator<, it's not needed
for your purposes, it's just something you must define to make set<Pair>
work. However (again rules of C++) you define operator< it must impose a
'strict weak ordering' on Pair (you sound reasonably mathematical so
I'll not define that). Here's on definition that does that.
bool operator<(const Pair& p1, const Pair& p2)
{
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
}

That should be enough for you to get a set of Pair's working. Now all
you have to do is write the code to form the catesian product, that is
just a couple of for loops one inside the other.

Hope this helps.

John
Feb 11 '07 #2
zf*****@umd.umich.edu wrote:
I have a program that creates two sets, one thru user interaction and
the other with the use of an array. Can anyone help with coding for
finding the cartesian product of the two sets; i.e a relation?

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{

int arr[] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29};
int arrSize = sizeof(arr) / sizeof(int);
set<intA;
set<intB(arr,arr+arrSize);

int num1=0;
int num2=0;

for(int x=0;x<20;x++)
{
cout << "Please enter an even integer less than 100:" << endl;
cin >num1;
if(A.find(num1) != A.end())

cout << num1 << " has already been entered!"
<< endl;

else if((num1 % 2) != 0)
cout << "That is not an even number!" << endl;
else
A.insert(num1);
}

cout << endl;

cout << "Set A has " << A.size() << "elements." <<endl;
cout << "Set B has " << B.size() << "elements." << endl;
system("pause");

return 0;


}

I have included the code below I got from a site that suggests
creating a struct pair and defining an operator, but I do not
understand it.

struct Pair{
int i;
int j;
};
int operator==(const Pair& p1,const Pair& p2){
return p1.i==p2.i && p1.j==p2.j;
}
set<pairresult;
Consider using std::pair<int,intinstead. The advantage is that is comes
with a correctly defined operator== and operator< so that you can use it
with std::set<>, i.e.,

std::set< std::pair<int,int

will be a set of ordered pairs of integers. Just what the doctor ordered.
Best

Kai-Uwe Bux
Feb 11 '07 #3

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

Similar topics

4
by: deancoo | last post by:
I need to do a Cartesian product, which is inherently expensive. Turns out, it's too expensive. I've dropped in that portion of my C++ code in hopes that someone with greater expertise with STL...
5
by: Ã…smund Kveim Lie | last post by:
Hi, We have found a possible bug in 7.3.1. It seems that using CROSS JOIN and doing plain Cartesian product, listing to tables in the from clause, gives different results. According to the...
7
by: Eric Slan | last post by:
Hello All: I'm having a problem that's been baffling me for a few days and I seek counsel here. I have an Access 2000 DB from which I want to run several reports. These reports are...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
44
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
5
by: thelightkeeper | last post by:
Hi, I have 1 table contains about 4 millions entries structure like below: ( AlarmID int, SetTime datetime )
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.