1. Write a CPP program that contains a function to exchange values of two arguments (swap) by using pointers and reference parameters.

/* SWAPING */


#include <iostream>
using namespace std;
void swap1(int *, int *);
void swap2(int &,int &);
int main()
 {
            int a,b;
            cout<<"Enter two integers: ";
            cin>>a>>b;
            cout<<"Initial Values: ";
            cout<<"a= "<<a<<" b= "<<b<<endl;
            swap1(&a,&b);
            cout<<"After Swapping(Pointers): ";
            cout<<"a= "<<a<<" b= "<<b<<endl;
            swap2(a,b);
            cout<<"After Swapping (Reference Parameters): ";
            cout<<"a= "<<a<<" b= "<<b<<endl;
            return 0;
}
 
void swap1(int *p, int *q)
{
            int temp;
            temp=*p;
            *p=*q;
            *q=temp;
}
 
void swap2(int &p, int &q)
{
            int temp;
            temp=p;
            p=q;
            q=temp;
}
Posted by Unknown On 01:41 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *