Thursday 26 June 2014

C++ program to swap two arrays

I have given 4 different c++ programs to swap two arrays. First program simply swaps the arrays using third variable , second program swaps the two arrays without using third variable, and third program swaps two arrays using call by reference and last program swap two arrays using pointers.


Method 1 : C++ program to swap two arrays using third variable
#include<iostream>
using namespace std;
int main()
{
    int a1[30],a2[30],num,temp;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array a1 : ";
    for(int i=0;i<num;i++)
        cin>>a1[i];
    cout<<"Enter elements of array a2 : ";
    for(int i=0;i<num;i++)
        cin>>a2[i];
    for(int i=0;i<num;i++)
    {
        temp=a1[i];
        a1[i]=a2[i];
        a2[i]=temp;
    }
    cout<<"After swapping "<<endl;
    cout<<"Array a1 : ";
    for(int i=0;i<num;i++)
        cout<<a1[i]<<" ";
    cout<<endl<<"Array a2 : ";
    for(int i=0;i<num;i++)
        cout<<a2[i]<<" ";
    return 0;
}



Method 2 : C++ program to swap two arrays without using third variable
#include<iostream>
using namespace std;
int main()
{
    int a1[30],a2[30],num,temp;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array a1 : ";
    for(int i=0;i<num;i++)
        cin>>a1[i];
    cout<<"Enter elements of array a2 : ";
    for(int i=0;i<num;i++)
        cin>>a2[i];
    for(int i=num;i<2*num;i++)
        a1[i]=a2[i-num];
    for(int i=0;i<num;i++)
        a2[i]=a1[i];
    for(int i=num;i<2*num;i++)
        a1[i-num]=a1[i];
    cout<<"After swapping "<<endl;
    cout<<"Array a1 : ";
    for(int i=0;i<num;i++)
        cout<<a1[i]<<" ";
    cout<<endl<<"Array a2 : ";
    for(int i=0;i<num;i++)
        cout<<a2[i]<<" ";
    return 0;
}




Method 3 : C++ program to swap two arrays using call by reference
#include<iostream>
using namespace std;
void swap(int (&a1)[30],int (&a2)[30],int num)
{
    int temp;
    for(int i=0;i<num;i++)
    {
        temp=a1[i];
        a1[i]=a2[i];
        a2[i]=temp;
    }
}
int main()
{
    int a1[30],a2[30],num;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array a1 : ";
    for(int i=0;i<num;i++)
        cin>>a1[i];
    cout<<"Enter elements of array a2 : ";
    for(int i=0;i<num;i++)
        cin>>a2[i];
    swap(a1,a2,num);
    cout<<"After swapping "<<endl;
    cout<<"Array a1 : ";
    for(int i=0;i<num;i++)
        cout<<a1[i]<<" ";
    cout<<endl<<"Array a2 : ";
    for(int i=0;i<num;i++)
        cout<<a2[i]<<" ";
    return 0;
}



Method 4 : C++ program to swap two arrays using pointers
#include<iostream>
using namespace std;
int main()
{
    int *p,*q,temp,n;
    cout<<"How many numbers to be entered in array : ";
    cin>>n;
    p=new int[n];
    q=new int[n];
    cout<<"Enter First array : ";
    for(int i=0;i<n;i++)
        cin>>p[i];
    cout<<"Enter Second array : ";
    for(int i=0;i<n;i++)
        cin>>q[i];
    cout<<"\nBefore swapping "<<endl<<"First Array : ";
    for(int i=0;i<n;i++)
        cout<<p[i]<<" ";
    cout<<endl<<"Second Array : ";
    for(int i=0;i<n;i++)
        cout<<q[i]<<" ";
    for(int i=0;i<n;i++)
    {
        temp=*(p+i);
        *(p+i)=*(q+i);
        *(q+i)=temp;
    }
    cout<<endl<<endl<<"After swapping "<<endl<<"First Array : ";
    for(int i=0;i<n;i++)
        cout<<p[i]<<" ";
    cout<<endl<<"Second Array : ";
    for(int i=0;i<n;i++)
        cout<<q[i]<<" ";
    cout<<endl;
    return 0;
}

No comments:

Post a Comment