Solved programming problem

Compare Two Array and save it to Another Array By save the small array in 1st in C++.

Source code

#include <iostream>

using namespace std;

int main()

{

    int array1[100],array2[100],i,n,m,count=0,j;

    int n_array[200];

    cout<<"Please Enter 1st Array Size: "<<endl;

    cin>>n;

    cout<<"Please Enter Array Element: ";

    for(i=0; i<n; i++)

    {

        cin>>array1[i];

    }

    cout<<"Please Enter 2nd Array Size: "<<endl;

    cin>>m;

    cout<<"Please Enter Array Element: ";

    for(i=0; i<m; i++)

    {

        cin>>array2[i];

    }

    if(n>m) ///check which array is bigger

    {

        for(i=0;i<m;i++)

         n_array[i]=array2[i];

         for(j=0;j<n;j++,i++)

            n_array[i]=array1[j];

    }

    else

    {

        for(i=0;i<n;i++)

         n_array[i]=array1[i];

         for(j=0;j<m;j++,i++)

            n_array[i]=array2[j];

    }

    cout<<"Total Element: "<<i<<endl;

    cout<<"Your Array: "<<endl;

     for(j=0; j<i; j++)

        cout<<n_array[j]<<" ";

        cout<<endl;

    return 0;

}

Output

Please Enter 1st Array Size: 9

Please Enter Array Element: -6 3 13 19 -27 5 0 8 9

Please Enter 2nd Array Size: 6

Please Enter Array Element:10 30 25 1 -9 20

Total Element: 15

Your Array: 10 30 25 1 -9 20 -6 3 13 19 -27 5 0 8 9

Join the discussion

Add your comment