Question

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 num2 = 7;

15

16 // Print the two

variable values

17 cout << "In main the two numbers are "

18 << num1 << " and " << num2 << endl;

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 << "Back in main again the two numbers are "

26 << num1 << " and " << num2 << endl;

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 << "In swapNums, after swapping, the two numbers are "

41 << a << " and " << b << endl;

42 }

The question I need an answer for is:

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. _______________________________________________________

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Modified C++ Program

// PUT YOUR NAME HERE.

#include <iostream>

using namespace std;

// 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;

}

OUTPUT

Explanation

When we are using reference parameters, then we are passing the memory address of the variables num1 and num2 declared in main. Hence in the function body when numbers are swapped then the process is directly reflected to the original memory address of the variables declared in main. As a result numbers are swapped in the function affecting the original variables in main.

Add a comment
Know the answer?
Add Answer to:
Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....
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
  • 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...

  • The function used in the following program is supposed to take 2 parameters (arguments) and switch...

    The function used in the following program is supposed to take 2 parameters (arguments) and switch the contents of the 2 and send them back to the calling program, but the values are not swapped and both have the same number in them. Select the option that fixes the problem? void interchange(int &arg1, int &arg2) { arg2 = arg1; arg1 = arg2 ; } int main() { int num1 = 4 , num2 = 5; interchange(num1, num2); cout << "Number...

  • So far your TestEqual functions have tested values but haven't made any changes... Q: If you...

    So far your TestEqual functions have tested values but haven't made any changes... Q: If you changed one of the variable values in your function, would it change the value of the variable in main? Why? Q: What are the two options for passing parameters into a function? (by _________ and by __________) What's the difference? Write a new function, MakeEqual, that takes two integers and if they are not equal (tested using your TestEqual function), displays a message and...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • 6. (Short answer) The C++ code below is very close to working, but, as written, won't...

    6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

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