Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I had to repost this question as someone have alreaydy answered this question fo

ID: 664978 • Letter: I

Question

I had to repost this question as someone have alreaydy answered this question for swapping but is there any other way to swap fuction in C++ using the same code below.

Given the following C program:

void swap(int **x, int **y) {

int *t=*x; *x=*y;*y=t;

}

int main (void) {

int i=5,j=7;

int *ip=&i; //int pointer ip gets the address of i

int *jp =&j; //int pointer jp gets the address of j

swap(&ip,&jp);

printf("i=%i j=%i",*ip,*jp);

return 0;

}

a. What is the parameter passing mechanism being used and what does the program print ?

b. Write the swap function in C++ so that it uses references to pointers instead of pointers to pointers

Explanation / Answer

A) The passing mechanism being used is pass by value. And the program prints the value of i and j after the swap. ie i=7 j=5

B)

The swap function in C++

#include<iostream>
#include<stdlib.h>

using namespace std;
void swap(int &x, int &y) {

int t=x; x=y;y=t;

}

int main (void) {

int i=5,j=7;
swap(i,j);

printf("i=%i j=%i",i,j);

return 0;

}