Answer to Question #323015 in C++ for Joyboy

Question #323015

Write a function in C++ program to exchange two numbers. 

These two variables are passed through parameters of the

function using pointer. void exchange(int *a, int *b)


1
Expert's answer
2022-04-03T13:30:42-0400
#include <iostream>
using namespace std;


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


int main() {
    int x = 3,y = 4;

    cout << "x = " << x << ";  y = " << y << endl;
    exchange(&x, &y);
    cout << "x = " << x << ";  y = " << y << endl;
    return 0;
}

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