#include <iostream> #include <string> using namespace std; void swap(int * a, in
ID: 639152 • Letter: #
Question
#include <iostream>
#include <string>
using namespace std;
void swap(int * a, int * b)
{
int temp = *a;
cout<<"the contents of the memories pointed to by 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<<"the contents of the memories pointed to by 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 5: What is the purpose of the
Explanation / Answer
5) & means you are sending address location of variables
* means pointing to particular memory location
6)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?
variables a and b are passed by reference so those will be same in function swap. so function swap will have same variables as in function main.
so both are same variables.
Question 7: 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?
Yes.. because those variables a and b are passed by reference so those will be same in function swap. so variables in function swap are same variables a and b of
function main. since we changed thier values in function swap, they will be swapped after function call in main.
8) pass by referrence
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.