I have two arrays.
- 1,3,5,7,9
- {3,5} or {1.9}. (left to right order)
So the second array is a subset of the first array But not a subset if the second array is {5.3} or, {9.1} (right to left order.)
My code is
#include <stdio.h>
void subset(int set11[], int set12[])
{
int length1;
int set1[] = {1, 5, 3, 9, 7, 0, 5};
length1 = sizeof(set1) / sizeof(int);
int length2;
int set2[] = {5, 9};
length2 = sizeof(set2) / sizeof(int);
int i = 0;
int j = 0;
int count = 0;
for (i = 0; i < length1; i++)
{
if (set1[i] == set2[j])
{
count = 1;
}
}
printf(" is \n");
if (count == 1)
{
printf("is subset");
}
else
{
printf("not subset");
}
}
int main()
{
int set11[] = {1, 5, 3, 9, 7};
int set12[] = {5, 9};
subset(set11, set12);
printf("");
return 0;
}
I get output in all cases only not subset.
count == 1;->count = 1;length1times but theset2array may not have that many elements which results in a buffer overrun.subset. Arrays degrade to pointers when you pass them in. That's whysizeof(set11)/sizeof(int)doesn't work.{1,2,3,4,5,6,7}and{3,3,5,5}is the latter considered a subset of the first even though there are repeating values? Or does the first array need to have multiple instances of 3 and 5 to qualify?