0

I have been trying to write code to find if an array is a proper, improper or not a subset. So far the code accurately tells when a subset is improper or not a subset. But it cannot tell when the subset is proper. Please point out mistakes and corrections.

#include <iostream>
using namespace std;

int main()
{
    int s;
    cout<<"Enter size: ";
    cin>>s;
    int *A = new int [s];
    
    int size1;
    cout<<"Enter subset size: ";
    cin>>size1;
    int *B = new int [size1];

        int i = 0;
        int j = 0;
        int count = 0;
        int check = 0;

        for (i = 0; i < size1; i++)
        {
            for (j = 0; j < s; j++)
            {
                if (B[i] == A[j])
                {
                    count++;
                }
            }
        }

        for (i = 0; i < size1; i++)
        {
            for (j = 0; j < s; j++)
            {
                if (B[i] == A[j])
                {
                    check = 0;
                    continue;
                }
                if (B[i] != A[j])
                {
                    check = 1;
                }
            }
        }

        if (count > 0 && check == 0)
        {
            cout << "\n\nIS a PROPER subset!\n";
            return 1;
        }
        if (check == 1 || j == size1 && count == 0)
        {
            cout << "\n\nNOT a subset!\n";
            return 0;
        }
        if (s == size1 && count > 0)
        {
            cout << "\n\nIS an IMPROPER subset!\n";
            return 2;
        }
        
        
}

For example:

The main array is A[4] = {1, 2, 3, 3}

  1. B[3] = {1,3,3} It is a proper subset (code tells wrong)
  2. B[2] = {1,2} It is a proper subset (code tells wrong)
  3. B[2] = {3,3} It is a proper subset (code tells wrong)
  4. B[3] = {1,3,4} No it is NOT a subset
  5. B[5] = {1,2,3,4,5} No it is NOT a subset
  6. B[3] = {3,3,4} No it is NOT a subset
  7. B[4] = {1,3,3,2} It is an improper subset

The code can tell when the subset is improper and when it has elements that are not part of the original set. But it doesn't tell when the array is an actual subset. Please advise.

  • 3
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/4641116). – Eljay Jul 27 '22 at 13:58
  • `int *A = new int [s];` — where is the corresponding `delete[]`? Also for `int *B = new int [size1];`… Hint No. 1: Never use `new` directly; you never need it (unless you are a low-level library author). Hint No. 2: Your second nested loops are just a pseudo-random generator of 0s or 1s, depending on how the arrays happen to be ordered. What you need (if you want to stick with this n×m complexity nightmare) is to iterate over the smaller array and try to find all of its elements in the bigger array. `if (all found) { if (array sizes equal) { improper } else { proper } } else { not a subset }`. – Andrej Podzimek Jul 27 '22 at 14:14
  • `count` tells you how many `B[i]` have a matching `A[j]`. But `[1,2,3,4,5]` and `[2,2,2,2,2]` would report 5. You aren't allowed to reuse the `2` multiple times, right? – Goswin von Brederlow Jul 27 '22 at 20:10
  • `check` is overwritten again and again. So it only tells you if `b[size - 1]` has a matching `A[j]` or not. I'm surprised the code does get any answer right. – Goswin von Brederlow Jul 27 '22 at 20:11

0 Answers0