Showing posts with label Swap Array Elements. Show all posts
Showing posts with label Swap Array Elements. Show all posts

Wednesday, July 6, 2011

Program to Swap the elements of two given Arrays


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int ar[100];
  int ar2[100];
  int r1,r2,i,t=0,l;
  for(i=0;i<100;i++)
  {
    ar[i]=0;
    ar2[i]=0;
  }
  cout<<"Enter the range of two arrays:(less than 100) "<<endl;
  cin>>r1>>r2;
  cout<<"Enter the elements of the first array:"<<endl;
  for(i=0;i<r1;i++)
  {
       cout<<"Enter element number:"<<(i+1)<<endl;
       cin>>ar[i];
  }
  cout<<"Enter the elements of the second array:"<<endl;
  for(i=0;i<r2;i++)
  {
       cout<<"Enter element number: "<<(i+1)<<endl;
       cin>>ar2[i];
  }
  if(r1<r2)
    l=r1;
  else
    l=r2;
  for(i=0;i<l;i++)
  {
         t=ar[i];
         ar[i]=ar2[i];
         ar2[i]=t;
  }
  cout<<"The swapped arrays are: \nfirst second \n");
  for(i=0;i<l;i++)
  {
    cout<<ar[i];
    cout<<ar2[i];
    cout<<"\n";
  }
  getch();
}