C++ question!!! Your application should ask the user to enter up to 10 positive
ID: 640909 • Letter: C
Question
C++ question!!!
Your application should ask the user to enter up to 10 positive integers (including 0) for each set, and then provide an answer that shows the union and the intersection of the sets.
Requirements:
Your application should ask the user to enter up to 10 positive integers (including 0) for each set, and then provide an answer that shows the union and the intersection of the sets.
In the answer, the elements must be in ascending order. Assume that the user will not enter repeated number within the set. Assume that the user may enter the numbers not in order.
The user may choose not to enter any integers, in which case the set will be considered empty.
The process should be repeated until the user decides to quit.
You can choose any format for the output, as long as:
It has a title o It contains a description of what the application does.
It is clearly explanatory o It is written correctly (no spelling/grammar/punctuation errors)
It clearly identifies the sets by using curly brackets and commas and by naming them A and B.
Explanation / Answer
#include < iostream.h > void main() {
cout << " Intersection set ";
char A[50];
//Array for First set.
char B[50];
//Array for First set.
char R[50];
//Resultant Variable.
int m;
//Variable for first set eliment size.
int n;
//Variable for second set eliment size.
int i;
//Loop index variable.
int j;
//Loop index variable.
int l = 0;
//Variable for result size.
char G = 'y';
int h;
while (G == 'y') {
cout << "Please enter 1 for Union set ";
cout << "Please enter 2 for Intersection set ";
cin >> h;
if (h == 1) {
cout << "Please enter number of elements of your first set: ";
//Getting input for first sets element size.
cin >> m;
//Storing size in variable.
cout << endl;
//getting new line
cout << "Please enter number of elements of your second set: ";
//Getting input for second sets element size.
cin >> n;
//Storing size in variable.
cout << endl;
//getting new line
for (i = 0; i < m; i++)
//loop for get input elements for first set.
{
cout << "Please enter element no " << i + 1 << " of your first set: ";
cin >> A[i];
}
cout << "Elements of your first set = { ";
for (i = 0; i < m; i++)
//loop for get input elements for first set.
{
cout << A[i];
if (i < m - 1) {
cout << " , ";
}
}
//loop printing second set elements.
cout << " }";
cout << " ";
for (i = 0; i < n; i++) {
cout << "Please enter element no " << i + 1 << " of your second set: ";
cin >> B[i];
}
cout << "Elements of your second set = { ";
for (i = 0; i < n; i++) {
cout << B[i];
if (i < n - 1) {
cout << " , ";
}
}
//loop printing second set elements.
cout << " }";
cout << " ";
//compute union
for (i = 0; i < m; i++) {
R[i] = A[i];
l++;
}
int e = l;
for (i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < e; j++) {
if (B[i] == R[j]) {
found = 1;
break;
}
}
if (found == 0) {
R[l] = B[i];
l++;
}
}
cout << "A Union B : { ";
for (i = 0; i < l; i++) {
cout << R[i] << " ";
}
cout << "}" << endl;
} else if (h == 2) {
cout << "Please enter number of elements of your first set: ";
//Getting input for first sets element size.
cin >> m;
//Storing size in variable.
cout << endl;
//getting new line
cout << "Please enter number of elements of your second set: ";
//Getting input for second sets element size.
cin >> n;
//Storing size in variable.
cout << endl;
//getting new line
for (i = 0; i < m; i++)
//loop for get input elements for first set.
{
cout << "Please enter element no " << i + 1 << " of your first set: ";
cin >> A[i];
}
cout << "Elements of your first set = { ";
for (i = 0; i < m; i++)
//loop for get input elements for first set.
{
cout << A[i];
if (i < m - 1) {
cout << " , ";
}
}
//loop printing second set elements.
cout << " }";
cout << " ";
for (i = 0; i < n; i++) {
cout << "Please enter element no " << i + 1 << " of your second set: ";
cin >> B[i];
}
cout << "Elements of your second set = { ";
for (i = 0; i < n; i++) {
cout << B[i];
if (i < n - 1) {
cout << " , ";
}
}
//loop printing second set elements.
cout << " }";
cout << " ";
//compute intersection
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i] == B[j]) {
B[j] = 0;
R[l] = A[i];
l++;
}
}
}
cout << "A Intersection B = { ";
for (i = 0; i < l; i++) {
cout << R[i] << " ";
}
cout << "}" << endl;
} else {
cout << "You entered wrong key ";
}
cout << " ";
cout << "Please enter 'y' for check again or 'n' for stop: ";
cin >> G;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.