Question

Last Coding Question! C++ is the programming language used This one is very difficult. My to...

Last Coding Question! C++ is the programming language used

This one is very difficult. My to do list is as follows:

// This program uses a function to swap the values in two variables.

/*TO DO:
change the function swapNums to use two pointer parameters instead of two reference parameters, and
then modify the complete program accodingly.
*/

#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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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;
}

Add a comment
Know the answer?
Add Answer to:
Last Coding Question! C++ is the programming language used This one is very difficult. My to...
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
  • 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...

  • Using a programming language of your choice, write a complete and fully functional program that uses...

    Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two integer numbers. The two numbers are read in separately by the program’s user. Use a proper prompt for each number read in. a. Use one function that uses formal parameter reference type to swap the two numbers b. Use another function that uses formal parameter pointer type to swap the two numbers. c. In the main or...

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

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

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

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

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

  • C++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

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