Problem Solving: Cross Product of Sets (Using Functions)
Learn to create a program that computes the cross-product of sets using functions.
We'll cover the following
main.cpp
Sets.txt
void loadSet(const char Msg[], int S[ ], int &size, ifstream &rdr);// Assume loadSet is available.void crossProduct(const char Msg[], int A1[],int sizeA1,int A2[],int sizeA2){// Write code here}int main(){const int capacity = 500;int A[capacity] = { 0 }, B[capacity] = { 0 }, sizeA, sizeB;ifstream rdr("Sets.txt");loadSet("A", A, sizeA, rdr); // load sizeA many entriesloadSet("B", B, sizeB, rdr); // load sizeB many entries// Write code for AXB: Do function call// Write code for BXA: Do function call}
Cross product
Here’s the prototype of the crossProduct()
function:
void crossProduct(const char Msg[],int A1[],int sizeA1,int A2[],int sizeA2)
We have called the two sets in the function A1
and A2
. The cross product can be taken of any two sets, be it AxB
or BxA
or any other two. The same implementation will work in all cases based on the two arguments passed.
Instruction: Look at the two implementations and write this function in the playground above to complete your program.
The previous implementation of cross product:
cout << "\nA x B: = {\n";
for (int ai = 0; ai <= sizeA-1; ai++)
{
cout << "\t\t\t";
for (int bi = 0; bi<=sizeB-1;bi++)
{
cout<<"("<<A[ai]<<", "
<< B[bi] << ")";
// for not print comma ',' at the end
if (bi!=(sizeB-1)||ai!=(sizeA-1))
cout << ", ";
}
cout << endl;
}
cout << "\nB x A: = {\n";
for (int bi = 0; bi <= sizeB-1; bi++)
{
cout << "\t\t\t";
for (int ai = 0; ai<=sizeA-1;ai++)
{
cout<<"("<<B[bi]<<", "
<< A[ai] << ")";
// for not print comma ',' at the end
if (bi!=(sizeB-1)||ai!=(sizeA-1))
cout << ", ";
}
cout << endl;
}
The crossProduct
function:
void crossProduct(const char Msg[],
int A1[],int sizeA1,int A2[],int sizeA2)
{
cout << Msg<<": = {\n";
for (int ai = 0; ai <= sizeA1-1; ai++)
{
cout << "\t\t\t";
for (int bi = 0; bi<=sizeA2-1;bi++)
{
cout<<"("<<A1[ai]<<", "
<< A2[bi] << ")";
// not print comma ',' at the end
if (bi!=(sizeA2-1)||ai!=(sizeA1-1))
cout << ", ";
}
cout << endl;
}
cout << "}";
}
int main()
{
.
.
.
crossProduct("AxB", A, sizeA, B, sizeB);
crossProduct("BxA", B, sizeB, A, sizeA);
.
.
.
}
Instruction: Add the function crossProduct()
and test them inside main()
and see the output.