-2

I am writing a program to check whether a set is a proper subset of a set or not. I am dynamically allocating memory for both of the sets(arrays) but after I provide one element the program stops executing.

   #include <stdio.h>
  int setID(int arr[],int arr2[],int size,int size2)
{
    int counter =0;
    for (int i=0; i<size2;i++)
    {
            if (arr2[i] == arr[i])
            {
                counter++;
            }


    }
    if (counter == (size2))
    {
        return 1;
    }
    else
        return 0;

}
int main ()
{
    printf("We are going to check if set A is a proper subset of B or not\n");
    printf("Please provide the cardinal number of set A \n");
    int a=0,b=0;

    scanf("%d",&a);
    int *p;
    p =(int*) malloc(a*sizeof(int));
    printf("Please provide the elements of Set A\n");
    for (int i=0;i<a;i++)
    {
        scanf("%d",p[i]);
    }
    printf("Please provide cardinal number for set B\n");
    scanf("%d",&b);
    int *p1;
    p1= (int*) malloc(b*sizeof(int));
    for (int i=0;i<b;i++)
    {
        scanf("%d",&p1[i]);
    }

    printf("Please note that 0 is false and 1 is true\n");
    printf("%d\n",setID(p,p1,a,b));
    return 0;

}

**Also have I passed the arguments correctly in the function: printf("%d\n",setID(p,p1,a,b)); **

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 2
    How is C++ related to the question? – eerorika Jan 31 '19 at 21:38
  • How do you know it stopped executing? Your shell prompt came back? You saw an error message? A debugger or other monitor told you it stopped with a certain exit code or signal? – aschepler Jan 31 '19 at 21:40
  • The C++ bigots were complaining. This *is* C-like, but that's really beside the point. One problem I see off the bat: `scanf("%d",p[i]);`: this should probably be `scanf("%d",&p[i]);`. – paulsm4 Jan 31 '19 at 21:43
  • Don't forget to consume those `\n` from the input. – Jite Jan 31 '19 at 21:46
  • `if (arr2[i] == arr[i])` was valid for all `int size,int size2`, you would only need to pass one size. Where is the guarantee that `size == size2`? – David C. Rankin Jan 31 '19 at 21:47
  • What are the input values ? if `a < b` you have illegal access, see my answer – bruno Jan 31 '19 at 21:47
  • 2
    I cannot help but note that this question, in which a program does not execute code that is wanted, complements [this recent question](https://stackoverflow.com/questions/54469299/why-my-code-repeat-itself-when-the-get-answer), in which a program does execute code that is not wanted. Therefore, on average, your two programs are correct. So clearly you should merge them and divide by two. – Eric Postpischil Jan 31 '19 at 21:58

2 Answers2

1

There is one error here

scanf("%d",p[i]);

which should be

scanf("%d", &p[i]);

and which is correct a few lines further down when you did this with p1. You say the program stops after providing one element and this is consistent with the error.

There may be other errors too as posted by others.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

in setID :

for (int i=0; i<size2;i++)
{
        if (arr2[i] == arr[i])
        {
            counter++;
        }
        ...

you suppose the size of arr is >= the size of arr2, but this is not mandatory because you read their size rather than to use the same when you allocate the arrays and read their values

if size < size2 (a < b in main) you go out of arr (p in main), the behavior is undefined

bruno
  • 32,421
  • 7
  • 25
  • 37