8.8 Lab: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is 3 8, then the output is 8 3 Your program must define and call a function. SwapValues returns the two values in swapped order. void SwapValues(int& userVal1, int& userVal2) zybooks c++ g

Respuesta :

Limosa

Answer:

Following are the program in c++ language

#include<iostream> // header file

using namespace std; // using namespace

void swapvalues(int&  userVal1, int& userVal2);// fucnction declaration

int main() // main method

{

   int x,y; // variable declaration

   cout<<"Enter the two value before swapping:\n";

   cin>>x>>y; // read the two input

  cout<<"before swapping:";

  cout<<x<<y; // display the value before swapping

   swapvalues(x, y);

   cout << "\nAfter swapping:";

   cout<<x<<y; // display the value after swapping

   return 0;

}

void swapvalues(int&  userVal1, int& userVal2) // function definition

{

   int t; // variable declaration

   t = userVal1; // assign the value userval1 to variable t

   userVal1 = userVal2; // assign the value userval2 to userval1

   userVal2 = t; // assign the value of variable t to userval2

}

Output:

Enter the two value before swapping:

3

8

before swapping:3 8

After swapping   :8 3

Explanation:

Following are the description of the program

  • Declared a variable "x" and "y" of type "int".
  • Read a input by user in x and y .
  • Display the values of variable x and y before the swapping.
  • Call the function  swapvalues() by passing the value of x and y.
  • After calling the console moved In the definition of swapvalues() Function where it interchanged the value of variable userVal1 and userVal2 by using third variable "t".
  • After interchanged the console moved to the main function and finally display the value of variable "x" and "y' after the swapping.

Following are C++ programs on Swapping values:

Program:

#include <iostream>//header file

using namespace std;

void SwapValues(int& userVal1, int& userVal2)//defining the method SwapValues that takes two integer variable

{

   int t;//defining integer variable t

   //performing the swapping

   t=userVal1;//using t variable that holds userVal1 value

   userVal1=userVal2;//using userVal1 variable that holds userVal2 value

   userVal2=t;//holding the t value in userVal2

   cout<<"After swapping value: "<<userVal1<<" "<<userVal2;//printing the swapped value

}

int main()//defining the main method

{

   int x,y;//defining integer variable that inputs value

   cout<<"Input first number: ";//print message

   cin>>x;//input value

   cout<<"Input second number: ";//print message

   cin>>y;//input value

   cout<<"Before swapping value: "<<x<<" "<<y<<endl;//printing value

   SwapValues(x,y);//calling method SwapValues

   return 0;

}

Program Explanation:

  • Defining the header file.
  • Defining the method "SwapValues" that takes two integer variables "userVal1 and userVal2" inside the parameter.
  • Inside the method, an integer variable "t" is declared that performs the swapping, and prints its value.
  • Outside the method, the main method is declared that defines two integer variables "x,y" and input the value from the user-end.
  • After that, we call the above method.  

Output:

Please find the attached file.

Find out more information about the swapping here:

brainly.com/question/19665043

Ver imagen codiepienagoya