adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C++ Copy the elements of two arrays into third one by one such that one element is copied from 1st array then one element is copied from 2nd array and so on.

Program in C++ Copy the elements of two arrays into third one by one such that one element is
 copied from 1st array then one element is copied from 2nd array and so on.




#include<iostream>
 using namespace std;
  int main()
   {
    const int size=25;
    int a[size],b[size],i,j,k,n,n1;
    cout<<"Enter the size of 1st array:-";
    cin>>n;
    cout<<"Enter the size of 2nd array:-";
    cin>>n1;
    if(n>size||n1>size)
       {
        cout<<"Size of array is invalid.";
        return 0;
  }
 cout<<"Enter "<<n<<" elements of 1st array :";
 for(i=0;i<n;i++)
   cin>>a[i];
 cout<<"Enter "<<n1<<" elements of 2nd array :";
 for(i=0;i<n1;i++)
   cin>>b[i];
 int c[n+n1];
    for(k=0,j=0,i=0;i<n+n1;i++)
  {
        if(i==0)
           c[i]=a[i];
      else if(i%2==0)
          c[i]=a[++k];
      else 
          c[i]=b[j++];
  }
  cout<<"Elements is ";
   for(i=0;i<n+n1;i++)
   cout<<"  "<<c[i];
 return 0;
   }
Output:-








Post a Comment

0 Comments