459,198 Members | 1,727 Online
Need help? Post your question and get tips & solutions from a community of 459,198 IT Pros & Developers. It's quick & easy.

# Cartesian product

 P: n/a 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 #include #include 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); setnum1; 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." <
2 Replies

 P: n/a 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 #include #include 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); setnum1; 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." < 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

 P: n/a 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 #include #include 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); setnum1; 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." <, i.e., std::set< std::pair

### This discussion thread is closed

Replies have been disabled for this discussion.