Problem 1: We sometimes use arrays to represent sets. For example S = {‘b’, ‘f’,
ID: 3814308 • Letter: P
Question
Problem 1: We sometimes use arrays to represent sets. For example S = {‘b’, ‘f’, ‘z’, ‘a’} is represented by the following array: ‘b’ ‘f’ ‘z’ ‘a’ You can use zero as a character, ‘0’, when an entry does not have any data. For example if we store S in an array of size 6: ‘b’ ‘f’ ‘z’ ‘a’ ‘0’ ‘0’ You can assume that arrays have a maximum size: 30 Write a C program that takes two arrays containing alphabetical characters and computes the following operations: - Union of the two arrays and output the result in a new array - Intersection of two arrays and output the result in a new array, - Is a set empty? Pleases note that you would an array to store the result that has twice the dimension of each array. If your input arrays have a dimension n, the resulting array should have 2n dimension. in C programing language
Explanation / Answer
//union,intersection,isempty
#include<stdio.h>
int main(){
int i,j;
char s1[10],s2[10];
char temp1[27],temp2[27];
char input1[10],input2[10];
char intersection[30],unions[30];
memset(s1,'0',10);
memset(s2,'0',10);
memset(temp1,'0',27);
memset(temp2,'0',27);
printf("Enter set 1 : ");
scanf("%s",input1);
printf("Enter set 2 : ");
scanf("%s",input2);
j=0;
for(i=0;i<10,input1[i]!='';i++){
if(input1[i]!=''){
s1[j++]=input1[i];
temp1[input1[i]-'a']=1;
}
}
j=0;
for(i=0;i<10,input2[i]!='';i++){
if(input2[i]!=''){
s2[j++]=input2[i];
temp2[input2[i]-'a']=1;
}
}
j=0;
for(i=0;i<26;i++){
if(temp1[i]==1 || temp2[i]==1){
unions[j++]=i+'a';
}
}
j=0;
for(i=0;i<26;i++){
if(temp1[i]==1 && temp2[i]==1){
intersection[j++]=i+'a';
}
}
printf("Set1 : %s",s1);
printf(" Set2 : %s",s2);
printf(" Union : %s",unions);
printf(" Intersection : %s",intersection);
return 0;
}