Thursday 26 June 2014

C++ program to check whether the given array is a subset of another array


Here is a C++ program to check whether the given array is a subset of another array


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    int a1[20],a2[20],num1,num2,flag=0;
    cout<<"How many elements to be stored in array a1 (max 20) : ";
    cin>>num1;
    cout<<"Enter elements of array a1 : ";
    for(int i=0;i<num1;i++)
        cin>>a1[i];
    cout<<"How many elements to be stored in array a2 (max 20) : ";
    cin>>num2;
    cout<<"Enter elements of array a2 : ";
    for(int i=0;i<num2;i++)
        cin>>a2[i];
    for(int i=0;i<num2;i++)
    {
        for(int y=0;y<num1;y++)
            if(a2[i]==a1[y])
            {
                flag=1;
                break;
            }
        if(flag==0)
        {
            cout<<"Array a2 is not a subset of array a1 ";
            return 0;
        }
        flag=0;
    }
    cout<<"Array a2 is a subset of array a1 ";
    return 0;
}


Any questions regarding to program please write in comments.

No comments:

Post a Comment