In Visual Studio Code under labactivity8_1 folder, create a new file main.cpp. Open the C++ source file main.cpp in the text editor and copy the source code below. /** * @file WRITE FILE NAME * @author WRITE STUDENT NAME(S) * @brief Using value and reference parameters. This program * uses a function to swap the values in two variables. * @date WRITE DATE TODAY * */ #include using namespace std; // Function prototype void swapNums(int number1, int number2); 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; } /** * @brief WRITE DESCRIPTION OF THE FUNCTION * * @param number1 DESCRIPTION * @param number2 DESCRIPTION */ void swapNums(int number1, int number2) { // Parameter a receives num1 and parameter b receives num2 // Swap the values that came into parameters a and b int temp = number1; number1 = number2; number2 = temp; // Print the swapped values cout << "In swapNums, after swapping, the two numbers are " << number1 << " and " << number2 << endl; } Update the header comment with the correct information. 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. It swapped the numbers in the function but did not return any values so the numbers won’t get swapped in main. 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. The function was passed references to the variables in main. This allows the function to change the contents of the variables in main as it is working on the same variable address in memory. Update the function header comments of swapNums.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Lab 8.1 - Using Value and Reference Parameters

  1. In Visual Studio Code under labactivity8_1 folder, create a new file main.cpp. Open the C++ source file main.cpp in the text editor and copy the source code below.

    /** * @file WRITE FILE NAME * @author WRITE STUDENT NAME(S) * @brief Using value and reference parameters. This program * uses a function to swap the values in two variables. * @date WRITE DATE TODAY * */ #include <iostream> using namespace std; // Function prototype void swapNums(int number1, int number2); 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; } /** * @brief WRITE DESCRIPTION OF THE FUNCTION * * @param number1 DESCRIPTION * @param number2 DESCRIPTION */ void swapNums(int number1, int number2) { // Parameter a receives num1 and parameter b receives num2 // Swap the values that came into parameters a and b int temp = number1; number1 = number2; number2 = temp; // Print the swapped values cout << "In swapNums, after swapping, the two numbers are " << number1 << " and " << number2 << endl; }
  2. Update the header comment with the correct information.

  3. 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. It swapped the numbers in the function but did not return any values so the numbers won’t get swapped in main.

  4. 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. The function was passed references to the variables in main. This allows the function to change the contents of the variables in main as it is working on the same variable address in memory.

  5. Update the function header comments of swapNums.

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education