Programming Assignment #1 CSE 215 1 Overview For this assignment, you\'re going
ID: 3596463 • Letter: P
Question
Programming Assignment #1 CSE 215 1 Overview For this assignment, you're going to write a C program to operate on sets. Our universe of elements consists of all lowercase letters. Your program should prompt the user for the elements of the first set (called A): the user enters the elements, followed by . Do the same for the second set (B). Then print the following information * The elements of A in the usual format: { ,e2, , en} . The elements of B The cardinality of A and B AUB AB 2 Details You should prompt the user for their two input strings . Remember, a set cannot contain more than one copy of an element. If the user enters an element twice, it should only appear in the set once. You may assume the elements of a set are entered without any spaces between them Remember to ignore any trailing newlines in your input.Explanation / Answer
Ans:
#include
#include
#include void main()
{
int i,j,k,p,ch,n1,n2,set1[10],set2[10], set3[20],flag;
char wish;
clrscr();
do
{
printf(“press 1 for union”);
printf(“ press 2 for intersection”);
printf(“ enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1://for union
printf(“ enter the size of set1 ”);
scanf(“%d”,&n1);
printf(“enter the element of set1 ”);
for(i=0;i<n1;i++)
scanf(“%d”,&set1[i]);
printf(“enter the size of set2 ”);
scanf(“%d”,&n2);
printf(“enter the elements of set2 ”);
for(i=0;i<n2;i++)
scanf(“%d”,&set2[i]);
k=0;
for(i=0;i<n1;i++)
{
set3[k]=set1[i];
k++;
}
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf(” %d”,set3[k]);
}
break;
case 2: //for intersection
printf(“enter the size of sets1”);
scanf(“%d”,&n1);
printf(“enter the element of set1”);
for(i=0;i<n1;i++)
scanf(“%d”,&set1[i]);
printf(“enter the size of sets2”);
scanf(“%d”,&n2);
printf(“enter the elements of set2”);
for(i=0;i<n2;i++)
scanf(“%d”,&set2[i]);
k=0;
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==0)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf(” %d”,set3[k]);
}
break;
}
printf(“ want to continue: “);
flushall();
scanf(“%c”,&wish);
}
while(wish!=’n’);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.