I cant figure this out. Here is my whole file, it is incomplete. If somebody has visual studio can you just paste it in and see if you can figure out why isSubset is not returning the right value?
I added printf statements to figure out where it was going and it doesnt even make sense to me. It is not even going to the if statement, yet it is returning 0.
Code:
#include<stdio.h>
int setCardinality(int a[]);
int isEmpty(int a[]);
int isMember(int x, int a[]);
int isSubset(int a[], int b[]);
void printSet(int a[]);
void setUnion(int a[], int b[]);
void setIntersection(int a[], int b[]);
int main()
{
int a[25];
int b[25];
int elementA = 0;
int elementB = 0;
int countA = 0;
int countB = 0;
int searchElement;
printf("Please enter your first set of numbers (enter -1 to end set)\n");
while (countA < 25)
{
scanf("%d",&elementA);
if (elementA == -1)
{
a[countA] = elementA;
break;
}
if (countA>=25){
break;
}
a[countA] = elementA;
countA++;
}
printf("Please enter your second set of numbers (enter -1 to end set)\n");
while (countB < 25)
{
scanf("%d",&elementB);
if (elementB == -1)
{
b[countB] = elementB;
countB++;
break;
}
if (countB>=25){
break;
}
b[countB] = elementB;
countB++;
}
printf("This is the first set you entered\n{");
printSet(a);
printf("}\n");
printf("This is the second set you entered\n{");
printSet(b);
printf("}\n");
printf("Number of elements in Set 1 is %d\n", setCardinality(a));
printf("Number of elements in Set 2 is %d\n", setCardinality(b));
if(isEmpty(a)==1){
printf("The set 1 entered by you is empty.\n");
}
if(isEmpty(b)==1){
printf("The set 2 entered by you is empty.\n");
}
if(isSubset(a,b)==1){
printf("Set 1 is a subset of set 2");
}
if(isSubset(a,b)==0){
printf("Set 1 is not a subset of set 2");
}
setUnion(a,b);
printf("Please enter the element you would like to search for in Set 1:\n");
scanf("%d",&searchElement);
if(isMember(searchElement, a)==1){
printf("The element %d was found in set 1!\n", searchElement);
}
if(isMember(searchElement, a)==0){
printf("The element %d was not found in set 1.\n", searchElement);
}
return 0;
}
int setCardinality(int a[]){
int i;
int numberElements=1;
if(a[0]==-1){
numberElements=0;
}
if(a[0]!=-1){
for(i=1;a[i]!=-1;i++){
numberElements++;
}
}
return (numberElements);
}
void printSet(int a[]){
int setCardinality(int a[]);
int j;
for(j=0;j<setCardinality(a);j++){
printf("%d,", a[j]);
}
}
int isEmpty(int a[]){
int setCardinality(int a[]);
if(setCardinality(a)==0){
return 1;
}
else{
return 0;
}
}
int isMember(int x, int a[]){
int setCardinality(int a[]);
int middle;
int low=0;
int high;
high=setCardinality(a);
while(low<=high){
middle = (low+high)/2;
if(x==a[middle]){
return 1;
}
else if(x<a[middle]){
high=middle-1;
}
else{
low=middle+1;
}
}
return 0;
}
int isSubset(int a[], int b[]){
int isMember(int x, int a[]);
int setCardinality(int a[]);
int i;
int search;
for(i=0;i<=setCardinality(a);i++){
search=a[i];
if(isMember(search,b)==0){
return 0;
printf("in if\n");
}
printf("in for, not in if\n");
}
printf("here after for\n");
return 1;
}