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

#include <iostream> #include <string> using namespace std; void swap(int & a, in

ID: 639153 • Letter: #

Question

#include <iostream>
#include <string>

using namespace std;

void swap(int & a, int & b)
{
   int temp = a;
   cout<<"a and b have the following values at the start of swap"<<endl;
   cout<<"a = "<<a<<" and b = "<<b<<endl;
   a=b;
   b=temp;
   cout<<"a and b have the following values at the end of swap"<<endl;
   cout<<"a = "<<a<<" and b = "<<b<<endl;

}
int main()
{
   int a=111, b=222;

   cout<<"a and b have the following values before swap is called"<<endl;
   cout<<"a = "<<a<<" and b = "<<b<<endl;
   swap(a,b);
   cout<<"a and b have the following values after swap is called"<<endl;
   cout<<"a = "<<a<<" and b = "<<b<<endl;
   return 0;
}

Question 3:   Are the variables a and b used in the function main the same a and b variables that are used in the function swap in the program in Step 2? Please explain your answer.

Question 4:   Were the values stored in the variables a and b in the function main swapped after the execution of the function swap in the program in Step 2? Explain your observations.

.

Explanation / Answer

3) no ..you are pasing a and b by swap(a,b)...a b values referrences are there...but &a, &b not means you are using same a and b there....if you use &x, &y instead of &a, &b..it works fine

4)

your code is basically pass by referrnce..so fron starting to ending the a and b are storing in same memory...so after swap function a and b values are modifies and stored in same memory location