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}
- B[3] = {1,3,3} It is a proper subset (code tells wrong)
- B[2] = {1,2} It is a proper subset (code tells wrong)
- B[2] = {3,3} It is a proper subset (code tells wrong)
- B[3] = {1,3,4} No it is NOT a subset
- B[5] = {1,2,3,4,5} No it is NOT a subset
- B[3] = {3,3,4} No it is NOT a subset
- 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.