Question

Implement the body of the swap function using the indirection operator // finish this program so...

Implement the body of the swap function using the indirection operator


// finish this program so that the values of
// x and y are swapped in main().
#include <iostream>
using namespace std;

void swap(int* first, int* second)
{
    // IMPLEMENT THIS FUNCTION
    // YOUR CODE GOES HERE
}

int main()
{
    int x = 9;
    int y = 2;

    swap(&x, &y);

    cout << "x: " << x << endl;
    cout << "y: " << y << endl;

    // if your program works, it will print:
    // x: 2
    // y: 9
}

Homework Answers

Answer #1


Given below is the completed code for the question. Please do rate the answer if it helped. Thank you.


// finish this program so that the values of
// x and y are swapped in main().
#include <iostream>
using namespace std;

void swap(int* first, int* second)
{
   int temp = *first;
   *first = *second;
   *second = temp;
  
}

int main()
{
   int x = 9;
   int y = 2;

   swap(&x, &y);

   cout << "x: " << x << endl;
   cout << "y: " << y << endl;

   // if your program works, it will print:
   // x: 2
   // y: 9
}

Know the answer?
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
Similar Questions
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1....
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1. Use print statements similar to the existing one (don't use assert). #include <iostream> using namespace std; // Function returns origNum cubed int CubeNum(int origNum) { return origNum * origNum * origNum; } int main() { cout << "Testing started" << endl; cout << "2, expecting 8, got: " << CubeNum(2) << endl; /* Your solution goes here */ cout << "Testing completed" << endl;...
4. Generate outputs "Hey, it's about time to go" using substr() function and stringA.at() operator. string...
4. Generate outputs "Hey, it's about time to go" using substr() function and stringA.at() operator. string stringA = 'hello, it's nice to meet you' (Do not take a short cut to print the output such as a = b;) #include <iostream> #include <string> using namespace std; int main() { /* Your solution goes here */ return 0; }
Please fill in the blank bolded below that would be the best choice for this program....
Please fill in the blank bolded below that would be the best choice for this program. #include <iostream> using namespace std; const int size = 100000; class TheBig { public: double operator[](int index) const {return (theData[index]);} private: double theData[size]; }; void firstToBeThe( ______________________________________________________) { for (int i = 0; i <size; i++) cout << value[i] <<endl; } int main() { TheBig one; firstToBeThe(one); }
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
11. 4.21 (What Does this Program Do?) What does the following program print? 1 // Exercise...
11. 4.21 (What Does this Program Do?) What does the following program print? 1 // Exercise 4.21: Mystery2.cpp 2 #include <iostream> 3 using namespace std; 4 5 int main() { 6 unsigned int count{1}; 7 8 while (count <= 10) { 9 cout << (count % 2 == 1 ? "****" : "++++++++") << endl; 10 ++count; 11 } 12 }
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type...
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type WidthFtIn. The function should assign WidthFtIn's data member numFeet with feetVal and numInches with inchesVal. #include <iostream> using namespace std; struct WidthFtIn {    int numFeet;    int numInches; }; /* Your code goes here */ int main() {    WidthFtIn objectSize;    int feetVal;    int inchesVal;    cin >> feetVal >> inchesVal;    objectSize = SetHeight(feetVal, inchesVal);    cout << "Size: "...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT