Question
Lab 6.6 Using Value and Reference Parameters Step 1: Remove areas4.cpp from the project and add the swapNums.cpp program in your Lab6 folder to the
Lab 6.6 Using Value and Reference Parameters
Step 1: Remove areas4.cpp from the project and add the swapNums.cpp program in your Lab6 folder to the project. 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
Step 2: Read the source code, paying special attention to the swapNums parameters. When the program is run do you think it will correctly swap the two numbers? Compile and run the program to find out.
Explain what happened. _________________________________________________________________
______________________________________________________________________________________
______________________________________________________________________________________
Step 3: Change the two swapNums parameters to be reference variables. Section 6.13 of your text shows how to do this. You will need to make the change on both the function header and the function prototype. Nothing will need to change in the function call. After making this change, recompile and rerun the program. If you have done this correctly, you should get the following output.
In main the two numbers are 5 and 7
In swapNums, after swapping, the two numbers are 7 and 5
Back in main again the two numbers are 7 and 5
Explain what happened this time. _________________________________________________________
______________________________________________________________________________________
______________________________________________________________________________________
You do not need to hand in the source code or output from this lab exercise.
The code given:
// Lab 6 swapNums.cpp -- Using Value and Reference Parameters // This program uses a function to swap the values in two variables. // PUT YOUR NAME HERE. #include
// Function prototype void swapNums(int, int);
/***** main *****/ int main() { int num1 = 5, num2 = 7; // Print the two variable values cout << "In main the two numbers are " << num1 << " and " << num2 << endl; // Call a function to swap the values stored // in the two variables swapNums(num1, num2); // Print the same two variable values again cout << "Back in main again the two numbers are " << num1 << " and " << num2 << endl; return 0; }
/***** swapNums *****/ void swapNums(int a, int b) { // Parameter a receives num1 and parameter b receives num2 // Swap the values that came into parameters a and b int temp = a; a = b; b = temp; // Print the swapped values cout << "In swapNums, after swapping, the two numbers are " << a << " and " << b << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started