2

I have two arrays.

  1. 1,3,5,7,9
  2. {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.

6
  • count == 1; -> count = 1; Commented Aug 2, 2021 at 4:06
  • Also, note that your logic is wrong. It declares a subset as soon as it finds any element is matching in the two arrays. That's wrong because any of the other elements could be non-matching. Even worse, the loop iterates length1 times but the set2 array may not have that many elements which results in a buffer overrun. Commented Aug 2, 2021 at 4:17
  • your answer is half Commented Aug 2, 2021 at 4:20
  • Note - you need to pass in the length of each array as function parameters to subset. Arrays degrade to pointers when you pass them in. That's why sizeof(set11)/sizeof(int) doesn't work. Commented Aug 2, 2021 at 4:30
  • If you have the arrays {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? Commented Aug 2, 2021 at 4:32

2 Answers 2

1

Applied some changes in logic. refer comments.

#include <stdio.h>
#include <stdbool.h>
void subset(int set11[], int set12[])
{
    int length1;
    int set1[] = {1,3,5,7,9};
    length1 = sizeof(set1) / sizeof(int);

    int length2;
    int set2[] = {3,5};
    length2 = sizeof(set2) / sizeof(int);

    int i = 0;
    bool isSubset = true;   //will indicate weather the second array is subset or not
//    int j = 0;    //
    int startPosition = 0;  // will decide the starting position for searching in  the main array.  {set 2}
//    int count = 0; //not needed; we will represent it with bool variable 'isSubset'.
    for (i = 0; i < length2; i++)   //Iterating through the subset array
    {
        bool isFound = false;
        for (int j=startPosition;j<length1;j++){        //Iterating through the original array {set 1}
            if (set2[i]==set1[j]){  //if element from second array is found in first array then...
                isFound = true;     //found the element
                startPosition = j+1;        //increasing the starting position for next search in the first array.
                printf("\t%d found at %d\n",set2[i],j);
                break;
            }
        }
        if(isFound==false){     //if not found then this is not subarray.
            printf("\t%d not found\n",set2[i]);
            isSubset = false;
            break;
        }
    }
//    printf(" is \n");
    if (isSubset)
    {
        printf("subset");
    }
    else
    {
        printf("not subset");
    }
}

int main()
{
    int set11[] = {1, 5, 3, 9, 7};
    int set12[] = {5, 9};
    subset(set11, set12);
    printf("");

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

excellent answer how is my question simply understanding or tough to understanding
Glad to help! For me, It was easy to understand ;)
0

You can run a nested loop to get hold of all the matching elements in the subset array

for (i = 0; i < length1; i++) 
{
    for(k = 0; k < length2; k++) 
    {
        if (set1[i] == set2[k])
        {
        count == 1;
        }
    }
}

Outer loop is for the for the First array Inner loop to check for the element at any position in the subset/second array

set1[] = {1, 5, 3, 9, 7, 0, 5}; set2[] = {5, 9};

If we run a nested loop we will get all the subsets regardless of their positions in the second array(set2)

it wont matter if the set2 is {5,9} or {9,5} in any case the counter variable will increase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.