In: Computer Science
C++
Given two finite sets, list all elements in the Cartesian product of these two sets.
Solution:
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
//finite sets
int M[3]= {1,2,3};
int N[3] = {4,5,6};
vector<pair<int,int>> Arr;
//iteration for cartesian product elements
for (int i = 0; i < sizeof(M)/sizeof(M[0]); i++)
{
for (int j = 0; j < sizeof(N)/sizeof(N[1]); j++)
{
Arr.push_back(make_pair(M[i],N[j]));
}
}
//to print elements of cartesian product
for (int i = 0; i < Arr.size(); i++)
{
cout <<"{"<< Arr[i].first << "," << Arr[i].second << "}"<<endl;
}
}
Thank you, Have a great day:-)