Answer to Question #272970 in C++ for Baneen

Question #272970

 Write C++ program to swap two input values using: a) Call by address function. b) Call by reference function.


1
Expert's answer
2021-11-29T02:46:37-0500
#include <iostream>
using namespace std;

void SwapByAddress(int* a, int * b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void SwapByReference(int& a, int& b) {
    int tmp = a;
    a = b;
    b = a;
}

int main() {
    int a, b;

    cout << "Enter a first integer: ";
    cin >> a;
    cout << "Enter a second integer: ";
    cin >> b;

    SwapByAddress(&a, &b);
    cout << "Swap by address: " << a << " " << b << endl;
    SwapByReference(a, b);
    cout << "Swap by reference: " << a << " " << b << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS