So I’m trying to write a program that finds the union, intersection, and determining if b is a subset of a for two groups of characters entered.
So if I enter
abcd for a[]
and
ac for b[]
It should print:
intersection: ac
union: abcd
“b is a subset of a”.
My code is working for the most part, but my union function isn’t giving an output. Do you have any suggestions for fixing it? I ran through it and it seems to be executing, but d isn’t printing for someone… is my boolean statement wrong?
My code:
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"
#define n 26
/* typedef enum letters {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
letters letter; */
bool intersection(bool a[], bool b[], bool c[]);
void GetSet(bool set[]);
void init(bool a[]);
void printArray(bool set[]);
void getunions(bool a[], bool b[], bool d[]);
void contain(bool a[], bool b[]);
main()
{
bool a[n], b[n], c[n], d[n];
init(a);
init(b);
init(c); /* intersection */
init(d); /* union */
printf("Entries for a = \n");
GetSet(a);
printf("Entries for b = \n");
GetSet(b);
intersection(a,b,c);
printArray(c);
getunions(a,b,d);
printArray(d);
contain(a,b);
getchar();
}
void init(bool set[])
{
int i;
for(i=0;i<n;i++)
{
set[i]=FALSE;
}
}
void GetSet(bool set[])
{
int i;
string str=GetLine();
int len=StringLength(str);
for(i=0;i<len;i++)
{
set[str[i]-97]=TRUE;
}
}
bool intersection(bool a[], bool b[], bool c[])
{
int i;
for(i=0;i<n;i++)
{
if(b[i]&&a[i]==TRUE) c[i]=TRUE;
}
printf("\n\nThe intersection is\n");
return c;
}
void getunions(bool a[], bool b[], bool d[])
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==TRUE||b[i]==TRUE)
d[i]==TRUE;
}
}
void printArray(bool set[])
{
int i;
for(i=0;i<n;i++)
{
if(set[i])
printf("%c", i+97);
}
}
void contain(bool a[], bool b[])
{
int i;
bool flag;
for(i=0;i<n;i++)
{
if(a[i]&&b[i]) flag=TRUE;
}
if(flag) printf("\n\nb is a subset of a.\n");
else printf("\n\nb is not a subset of a.\n");
}
In
getunions,d[i]==TRUE;should bed[i]=TRUE;. You want an assignment, not a comparison.As an aside, I think the
containfunction is incorrect. A) it does not initializeflag, and B) it says “b is a subset of a” as long as a and b share at least one common element.