Ask Question
28 September, 07:39

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

+2
Answers (1)
  1. 28 September, 08:05
    0
    Following are the program in c+ + language

    #include / / 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<
    swapvalues (x, y);

    cout << "/nAfter swapping:";

    cout<
    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers