#include using namespace std;void Swap(int x,int y); //prototypeint main(){ int num1, num2; cout << "Please enter two numbers to swap, the same numbers will quit." << endl; cin >> num1 >> num2; while (num1 != num2) { Swap(num1,num2); cout << "After swap, the numbers are " << num1 << " " << num2 << endl; cout << "Please enter two numbers to swap, "; cout << "the same numbers will quit." << endl; cin >> num1 >> num2; } cout << "Thanks!" << endl; return 0; }//***************************//This subroutine swaps two numbers//*****************************void Swap(int x, int y){ int temp; temp = x; x = y; y = temp;}1. Type in the above program as swap.cpp. Add a comment to include your name and date. Compile and run with different values.2. To find out why this is not working, add the following output statements to the swap function.a. cout << " beginning of swap" << x << " " << y << endl;b. cout << " end of swap" << x << " " << y << endl;Are the variables being swapped inside the swap function?3. Explain why this program is not working correctly.4. Fix the program so the swap will work. Compile and run and be sure the swap works.5. Submit a copy of the final listing and output. Also include a copy of the hierarchy chart for the program.

Respuesta :

Answer:

Fix:

void Swap(int &x, int &y){

           int temp;

           temp = x;

           x = y;

           y = temp;        }

See the explanation

Explanation:

1. swap.cpp

//Name: ABC    Date: dd/mm/yyyy

#include

using namespace std;

void Swap(int x,int y); //prototype

int main(){

   int num1, num2;

   cout << "Please enter two numbers to swap, the same numbers will quit." << endl;

   cin >> num1 >> num2;    

   while (num1 != num2) {

       Swap(num1,num2);

       cout << "After swap, the numbers are " << num1 << " " << num2 << endl;

       cout << "Please enter two numbers to swap, ";

       cout << "the same numbers will quit." << endl;

       cin >> num1 >> num2; }

       cout << "Thanks!" << endl;

       return 0; }

//********************//This subroutine swaps two numbers // ********************

       void Swap(int x, int y){

           int temp;

           temp = x;

           x = y;

           y = temp;}

The output of this program with num1 = 1 and num2 = 2 is:

Please enter two numbers to swap, the same numbers will quit.                 1                                                                                                                               2                                                                                                                               After swap, the numbers are 1 2                                                                         Please enter two numbers to swap, the same numbers will quit.                 1                                                                                                                           

1                                                                                                                               Thanks!  

Notice that the numbers 1 and 2 are not swapped correctly even when the Swap() method is called in the main() function.

The other numbers 1 and 1 are entered in order to quit the program.

 

2.

In this part first output statement a) is added to the start of Swap() method before swapping the numbers x and y and the output statement b) is added to the last after the set of operations performed to swap x and y.

   void Swap(int x, int y){

       cout << " beginning of swap " << x << " " << y << endl;

           int temp;

           temp = x;

           x = y;

           y = temp;

           cout << " end of swap " << x << " " << y << endl;        }

Lets say x = 1 and y = 2

The output of these two statements is:

beginning of swap 1 2      

end of swap 2 1    

This shows that the Swap() function is working correctly and swapping the two numbers x and y.

3.    

The reason that the program is not working properly is that x and y are value parameters. They are like local variables and local to the Swap() function. So changes made to them have no effect on the calling functions arguments num1 and num2. That is why the result remains the same when this function is called in the main().

In these three statements of Swap() method:

int temp = x;

   x = y;      

   y = temp;    

temp is a local variable which is local to this Swap function

Similarly x=y; and y= temp statements make changes only in the local copy and not in the values of num1 and num2 which are passed to this function in main()

So for the above example where num1 = 1 and num2 = 2, the statement

 cout << "After swap, the numbers are " << num1 << " " << num2 << endl;

displays:

After swap, the numbers are 1 2

This means their real values are printed as it is without swapping the values.

4.

The solution is to mark the parameters x and y of the function Swap() as reference parameters in the following way:

void Swap(int &x, int &y)

In this way the memory address of each argument num1 and num2 is passed to the Swap(). The Swap() uses this address to both access and change the values.

So   Swap(num1,num2); function call passes the memory address the actual parameters.

For the reference parameter an ampersand (&) is used before the variables x and y of Swap method. Now any changed made to these variables will effect the calling function arguments num1 and num2. So if x and y are swapped then num1 and num2 are also swapped when this function is called in main().

So change the prototype of Swap function as:

void Swap(int &x,int &y);

Change the function definition as:

void Swap(int &x, int &y){

           int temp;

           temp = x;

           x = y;

           y = temp;        }

Now the program gives the following output:

Please enter two numbers to swap, the same numbers will quit.                                                                                 1                                                                                                                                             2                                                                                                                                             After swap, the numbers are 2 1                                                                                                               Please enter two numbers to swap, the same numbers will quit.                                                                                 1                                                                                                                                        1                                                                                                                                             Thanks!  

See now the numbers are correctly swapped. The program and its output is attached.

Ver imagen mahamnasir