Ask Question
18 March, 12:04

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)

#include

/ * Define your function here * /

int main (void) {

/ * Type your code here. Your code must call the function. * /

return 0;

+4
Answers (1)
  1. 18 March, 13:52
    0
    The program to this question can be given as:

    Program:

    #include / /header file

    using namespace std; / /using namespace.

    void SwapValues (int * userVal1, int * userVal2); / /function declaration.

    void SwapValues (int * userVal1, int * userVal2) / /function definition.

    { / /function body.

    //perform swapping

    int t = * userVal1;

    *userVal1 = * userVal2;

    *userVal2 = t;

    }

    int main () / /main method

    {

    int n1, n2; / /define variable

    cout<<"Enter first number : "; / /message

    cin>>n1; / /input by user.

    cout<<"Enter second number : "; / /message

    cin>>n2; / /input by user.

    SwapValues (&n1,&n2); / /calling function.

    cout<<"Swapped values"<
    cout<<"first number is : "<
    cout<<"second number is:"<
    return 0;

    }

    Output:

    Enter first number : 3

    Enter second number : 8

    Swapped values

    first number is : 8

    second number is : 3

    Explanation:

    The description of the above C+ + language program can be given as:

    In the program, firstly we include the header file. Then we declare and define a function that is "SwapValues () " function in the function we pass two integer variable that is "userVal1 and userVal2" inside a function, we define an integer variable that is "t" and perform swapping. Then we define the main function in the main function we define two variables that is "n1 and n2" this variable is used to take value-form user. then we pass this value to function and print the function values.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers