Demo entry 6687002

vcds

   

Submitted by anonymous on Dec 26, 2017 at 04:05
Language: C++. Code size: 1.3 kB.

//swapping with reference and with pointers    
#include<iostream>    
using namespace std;    
void swapr(int& a,int& b);    
void swapp(int* p,int* q);    
void swapv(int a, int b);    
    
int main(){    
    int wallet1=300;    
    int wallet2=350;    
    cout<<"wallet1=$"<<wallet1    
        <<" , wallet2=$"<<wallet2<<endl;    
    
    cout<<"using reference to swap contents:"<<endl;    
    swapr(wallet1,wallet2);    
    cout<<"wallet1=$"<<wallet1    
        <<" , wallet2=$"<<wallet2<<endl;    
    
    cout<<"using pointers to swap contents:"<<endl;    
    swapp(&wallet1,&wallet2);    
    cout<<"wallet1=$"<<wallet1    
        <<" , wallet2=$"<<wallet2<<endl;    
    
    cout<<"using values to swap contents:"<<endl;    
    swapv(wallet1,wallet2);    
    cout<<"wallet1=$"<<wallet1    
        <<" , wallet2=$"<<wallet2<<endl;    
    
    return 0;    
}    
    
void swapr(int& a,int& b){    
    int temp;    
    temp=a;    
    a=b;    
    b=temp;    
}    
    
void swapp(int* p,int* q){    
    int temp;    
    temp=*p;    
    *p=*q;    
    *q=temp;    
}    
    
void swapv(int a, int b){    
    int temp;    
    temp=a;    
    a=b;    
    b=temp;    
}    

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).