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

Complete the test by value program (see Listing 6.5) on page 213 of the textbook

ID: 3804649 • Letter: C

Question

Complete the test by value program (see Listing 6.5) on page 213 of the textbook. Your output should look like that on page 213 of the textbook (see below).

Before invoking the swap method, num1 is 1 and num2 is 2

       Inside the swap method

              Before swapping n1 is 1 n2 is 2

After swapping n1 is 2 n2 is 1

After invoking the swap method, num1 is 1 and num2 is 2

Next, modify the main method so after the call to the swap method the actual values of num1 and num2 are swapped in main and show this in the output. Your output will have one additional line after the listing on page 213 that shows the values of the two numbers in main after they are swapped. See new output line below in blue text.

Before invoking the swap method, num1 is 1 and num2 is 2

       Inside the swap method

              Before swapping n1 is 1 n2 is 2

After swapping n1 is 2 n2 is 1

After invoking the swap method, num1 is 1 and num2 is 2
After swapping the numbers in main method, num1 is 2 and num2 is 1.

Explanation / Answer

first part:

#include<iostream>
#include<cmath>
#include<string>
using namespace std;


void swap(int n1,int n2)
{
cout<<"inside swap method"<<endl;
cout<<"Before swapping n1 is " <<n1 <<" n2 is "<<n2<<endl;
int t=n1;
n1=n2;
n2=t;
cout<<"after swapping n1 is " <<n1 <<" n2 is "<<n2<<endl;
}
int main()
{
int num1=1;
int num2=2;
cout<<"Before invoking swap method,num1 is "<<num1<<" and num2 is "<<num2<<endl;
swap(num1,num2);
cout<<"after invoking swap method,num1 is "<<num1<<" and num2 is "<<num2<<endl;
return 0;

}

second part:

#include<iostream>
#include<cmath>
#include<string>
using namespace std;


void swap(int* n1,int* n2)
{
cout<<"inside swap method"<<endl;
cout<<"Before swapping n1 is " <<*n1 <<" n2 is "<<*n2<<endl;
int t=*n1;
*n1=*n2;
*n2=t;
cout<<"after swapping n1 is " <<*n1 <<" n2 is "<<*n2<<endl;
}
int main()
{
int num1=1;
int num2=2;
cout<<"Before invoking swap method,num1 is "<<num1<<" and num2 is "<<num2<<endl;
swap(&num1,&num2);
cout<<"after invoking swap method,num1 is "<<num1<<" and num2 is "<<num2<<endl;
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote