Answer to Question #243941 in C++ for ASAD

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.


1
Expert's answer
2021-09-29T01:18:39-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS