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 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 num2 = 7; 15 16 // Print the two variable values 17 cout 18 19 20 // Call a function to swap the values stored 21 // in the two variables 22 swapNums(num1, num2); 23 24 // Print the same two variable values again 25 cout 26 27 28 return 0; 29 } 30 31 /***** swapNums *****/ 32 void swapNums(int a, int b) 33 { // Parameter a receives num1 and parameter b receives num2 34 // Swap the values that came into parameters a and b 35 int temp = a; 36 a = b; 37 b = temp; 38 39 // Print the swapped values 40 cout 41 42 }
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 using namespace std;
// Function prototype void swapNums(int, int);
/***** main *****/ int main() { int num1 = 5, num2 = 7; // Print the two variable values cout // Call a function to swap the values stored // in the two variables swapNums(num1, num2); // Print the same two variable values again cout 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 }
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