Question
This is a C++ statement, how do I solve this problem using the void function?
Question 29 10 pts (SHORT ANSWER) Define a void function named swap2 Nums that accepts 2 integer reference parameters: num1 a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :- The following C++ code swap the 2 numbers inside a function swap2Nums with the help of the third variable.The neccessary instructions are mentioned through comments in the code.

The code is :-

#include <iostream>
using namespace std;

// function definition function named as swap2Nums and taking the refrence parameters
void swap2Nums(int &a, int &b) {
    // initialising the third variable temp_var
    int temp_var;
    temp_var = a;
    a = b;
    b = temp_var;
}

int main()
{
    int num1,num2;
    //Take input of variables from user
    cin>>num1;
    cin>>num2;
    
    cout << "Before swapping the numbers are :-" << endl;
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

    // call the  function to swap the both numbers
    swap2Nums(num1,num2);
    
    // printing the value of both numbers after the swap
    cout << "After swapping the numbers are :-" << endl;
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

    return 0;
}

The output is :

Status Successfully executed Date 2020-07-31 18:39:15 Time 0 sec Mem 15.232 kB Input 2 3 Output Before swapping the numbers a

The code screenshot is :-

1 #include <iostream> 2 using namespace std; 3 4 // function definition function named as swap2Nums and taking the refrence p

Add a comment
Know the answer?
Add Answer to:
This is a C++ statement, how do I solve this problem using the void function? Question...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....

    Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...

  • python programming Question 29 10 pts (SHORT ANSWER) Define a function named display_pattern that is a...

    python programming Question 29 10 pts (SHORT ANSWER) Define a function named display_pattern that is a void function with no parameters. When the function is called, it should use a nested loop to to display the symbol # in 2 rows and 3 columns as follows: ### ### BI V A - A - I E33 x x, E V GT 12pt Paragraph

  • Project Execute Tools AStyle Window Help 456 TDM-GCC 4.9.2 32-bit Profiling I"l practicel swapNums-KEY.cpp This progran uses a function to swop the values in two variables. 1 3 TO DO: 4 change th...

    Project Execute Tools AStyle Window Help 456 TDM-GCC 4.9.2 32-bit Profiling I"l practicel swapNums-KEY.cpp This progran uses a function to swop the values in two variables. 1 3 TO DO: 4 change the function swap Nuns to use two pointer parameters instead of two reference parameters, and 5 then modify the complete program accodingly <iostream > #include 8 9 using namespace std; 10 void swapNums (int&, int&) 12 int main() 13 14 15 int numl5, num2 = 7; 16 cout...

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • I'm stuck on this question and I have a hard time figuring this out, this used...

    I'm stuck on this question and I have a hard time figuring this out, this used as C++ Question 26 10 pts (SHORT ANSWER) Use a loop to calculate and print the sum of numbers from 1 to 10 (both numbers inclusive). Use additional variable(s) as necessary. For example your output should display as follows: Sum of numbers 1-10 = 55 BIVA-A-I EI 1 XX, EE 2.2 VX T 1 12pt - Paragraph O words

  • C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named...

    C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named god() that accepts two integers as input parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers, a and b, is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and o is that number. One efficient...

  • This problem is considered from C++, I need to know the steps using the step-by-step function...

    This problem is considered from C++, I need to know the steps using the step-by-step function for me to fully understand on how to solve this tough problem. Question 30 10 pts (SHORT ANSWER) Write three separate if blocks to check the following THREE conditions (number your answers): 1. If the number variable testScore is outside the range of O through 100 (excluding both those scores), print the message: "INVALID: OUTSIDE RANGE 0-100" 2. If the number variable bonusPoints is...

  • write code using void function i need the code in C++ but make sire u solve...

    write code using void function i need the code in C++ but make sire u solve by using void function visor (GCD) of two integers is the largest nacd that returns the greatest com- its digits reversed. For example, 5.31 (Greatest Common Divisor) The greatest common divisor (GCD) of two in integer that evenly divides each of the numbers. Write a function gcd that returns the mon divisor of two integers. . . for Numeric Grades) Write a function qualityPoints...

  • Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

    Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write...

  • This is a C++ statement, when it comes to Algebra, this is very difficult for me...

    This is a C++ statement, when it comes to Algebra, this is very difficult for me to solve this, please help me solve this problem with step-by step solutions Question 27 10 pts (SHORT ANSWER) Write C++ statements to do the following (number your answers): 1. Declare a constant PI and initialize it with 3.1415 2. Declare a variable volume of type double 3. Calculate the volume of the cone and store it in the variable volume . Given the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT