Question #243941

Write a function called swap() that interchanges two int values passed to it by the calling program. (Note that this function swaps the values of the variables in the calling program, not those in the function.) You’ll need to decide how to pass the arguments. Create a main() program to exercise the function.


Expert's answer

#include <iostream>
using namespace std;
void swap(int &a, int &b){
    int c = a;
    a = b;
    b = c;
}
int main(){
    int a = 5, b = 8;
    printf("Before swapping\t a = %d; b = %d", a, b);
    swap(a, b);
    printf("\nAfter swapping\t a = %d; b = %d", a, b);
    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!

LATEST TUTORIALS
APPROVED BY CLIENTS