Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey I\'m trying to learn how to write a program where you have two string arrays

ID: 3628965 • Letter: H

Question

Hey I'm trying to learn how to write a program where you have two string arrays and you output the union of the strings and the intersection of the strings without repeating words. Here is an example of some array elements.

- Union of the sets A and B, denoted A ? B, is the set of all objects that are a member of A, or B, or both. The union of {a, b, c} and {e, b, c} is the set {a, b, c, e}.
- Difference of two sets A and B (also known as the set-theoretic difference of A and B, or the relative complement of B in A) is the set of elements that are in A but not in B. The difference of {a, b, c, d} and {b, d, e, f} is the set {a, c}. In your program, you need to support all four set operations. The input sets of A and B are going to be entered by the user and need to be stored into array named with “SetAArray” and “SetBArray”. Sample input sets of A and B are listed below. Your program need to perform all two operations when the user entered the sets of A and B.

Three test samples (input sets) for A and B,
- Sample 1
A = {a, b, c, d, e}
B = {a, d, c, f, g, k, i}
(The union should be abcdefgki) (The intersection is acd)

- Sample 2
A = {John, Tom, Paul, Larry, Zach}
B = {Tom, Larry, Chen, Flower, Charlotte, John, Rory}
(The union should be John, Tom, Paul, Larry, Zach, Chen, Flower, Charlotte, Rory)
(The intersection is Tom, Larry)

- Sample 3
A = {USA, Canada, Italy, Mexico, India}
B = {Canada, Mexico, Thailand, England, Italy, India}
(The Union should be USA, Canada, Italy, Mexico, India, Thailand, England)
(The intersection is Canada, Italy, Mexico)

Explanation / Answer

#include <conio.h>
#include <stdio.h>
#include <process.h>
void main()
{
int i,j,k,p,ch,n1,n2,setAArray[10],setBarray[10], set3[20],flag;
char wish;
clrscr();
do
{
cout<< "press 1 for union";
cout<< " press 2 for intersection";
cout<< " press 3 for subtraction";
cout<< " enter ur choice";
cin>>ch;
switch(ch)
{
case 1://for union
cout<<" enter the size of set a ";
cin>>n1;
cout<<"enter the element of set a ";
for(i=0;i<n1;i++)
cin>>setAArray[i];
cout<<"enter the size of set b ";
cout<<n2;
cout<<"enter the elements of set b ";
for(i=0;i<n2;i++)
cin>>setBarray[i];
k=0;
for(i=0;i<n1;i++)
{
set3[k]=setAArray[i];
k++;
}
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(setBarray[i]==setAArray[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=setBarray[i];
k++;
}
}
p=k;
for(k=0;k<p;k++)
{
cout<<set3[k];
}


break;
case 2: //for intersection
cout<<"enter the size of set a";
cin>>n1;
cout<<"enter the element of set a";
for(i=0;i<n1;i++)
cin>>setAArray[i];
cout<<"enter the size of set b ";
cin>>n2;
cout<<"enter the elements of set b";
for(i=0;i<n2;i++)
cin>>setBarray[i];
k=0;
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(setBarray[i]==setAArray[j])
{
flag=0;
break;
}
}
if(flag==0)
{
set3[k]=setBarray[i];
k++;
}
}
p=k;
for(k=0;k<p;k++)
{
cout<<set3[k];

}
break;
case 3://for subtraction
cout<<"enter the size of sets1";
cin>>n1;
cout<<"enter the element of set a";
for(i=0;i<n1;i++)
cin>>setAArray[i];
cout<<"enter the size of set b";
cin>>n2;
cout<<"enter the elements of set b ";
for(i=0;i<n2;i++)
cin>>setBarray[i]);
k=0;
for(i=0;i<n1;i++)
{
flag=1;
for(j=0;j<n2;j++)
{
if(setAArray[i]==setBarray[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=setAArray[i];
k++;
}
}
p=k;
for(k=0;k<p;k++)
{
cout<<set3[k];

}
break;

}


cout<<" want to continue: ";
flushall();
cin>>wish;
}
while(wish!='n');
} //prg terminates

/* The program is in its simplest form. A lot of effort can be reduced by making defining a function say set and making a call whenever value needs to be entered. The program is simple but if you still have a doubt message me */