Using Value and Reference Parameters 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.

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.

  6. Commit your code with a message "lab 8.1 completed". Recall the terminal commands:

      • git add -A
      • git commit -m "COMMIT MESSAGE"
  7. Push your code back to the GitHub repo then continue with Lab 8.2.

Lab 8.2 - Three File Structure 1 

  1. We will keep working with Lab 8.1 program. Create a new file called functions.h. Inside this file, you will have to write the guard code, to ensure the code inside this file is only included once. The guard code is demonstrated below. Copy the code below and paste it inside functions.h.

    /** * @file functions.h * @author WRITE STUDENT NAME(S) * @brief Declaration file * @date WRITE DATE TODAY * */ #ifndef FUNCTIONS_H // GUARD CODE #define FUNCTIONS_H // GUARD CODE // INCLUDE THE HEADER FILES NEEDED FOR THE FUNCTIONS TO WORK using namespace std; // PUT FUNCTION PROTOTYPES HERE #endif // GUARD CODE
  2. Update the header comment with the correct information.

  3. From main.cpp, copy and remove the function prototypes, then paste the function prototypes in functions.h.

  4. Back in main.cpp, just above using namespace std, you now need to include functions.h. Observe the code below.

    #include <iostream> #include "functions.h" using namespace std;
  5. Create another new file called functions.cpp. This file is where the function definitions are going to be placed. Note, that the first line of code in this file is to include functions.h.

    /** * @file functions.cpp * @author WRITE STUDENT NAME(S) * @brief Implementation/Definition file * @date WRITE DATE TODAY * */ #include "functions.h" // PUT FUNCTION DEFINTIONS HERE
  6. Update the header comment with the correct information.

  7. From main.cpp, copy and remove the function definitions, then paste the function definitions in functions.cpp. Observe that you are using cout inside the function, which means you need to include iostream. The right place to include iostream will be inside functions.h.

  8. Compile the program. For the executable named Lab8_2 see command below

    • Use the compile command g++ -Wall lab8_1.cpp functions.cpp -o Lab8_2
  9. Run the program. The program should give you the same result as in Lab 8.1. Understand how this code organization could help in the maintainability of your code. You will see the importance of this type of code organization when you have lots of functions in your C++ project.

  10. Commit your code with the message "lab 8.2 completed".

Expert Solution
steps

Step by step

Solved in 3 steps

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