Question

In: Computer Science

Please write the following swap functions and print code in main to show that the functions...

Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values.

1) swap integer values using reference parameters

2) swap integer values using pointer parameters

3) swap pointers to integers - you need to print the addresses, not the integers.

Solutions

Expert Solution

Following is the code in swap.cpp :

#include <iostream>

using namespace std;


void swap_by_reference(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

void swap_by_pointers(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void swap_pointers(int **a, int **b)
{
    int *temp = *a;
    *a = *b;
    *b = temp;
}


int main()
{
    int a, b;

    
    /*
     * Reinitialize a, b.
     */
    a = 42;
    b = 786;

    cout << endl << endl;
    cout << "Before SWAP-BY-REFERENCE, a = [" << a << "], b = [" << b << "]" << endl;
    swap_by_reference(a, b);
    cout << "After SWAP-BY-REFERENCE, a = [" << a << "], b = [" << b << "]" << endl;



    /*
     * Reinitialize a, b.
     */
    a = 13;
    b = 111;

    cout << endl << endl;
    cout << "Before SWAP-BY-POINTERS, a = [" << a << "], b = [" << b << "]" << endl;
    swap_by_pointers(&a, &b);
    cout << "After SWAP-BY-POINTERS, a = [" << a << "], b = [" << b << "]" << endl;



    int *pa = &a;
    int *pb = &b;

    cout << endl << endl;
    cout << "Before SWAP-POINTERS, pa = [" << pa << "], pb = [" << pb << "]" << endl;
    swap_pointers(&pa, &pb);
    cout << "After SWAP-POINTERS, pa = [" << pa << "], pb = [" << pb << "]" << endl;

    cout << endl << endl;
    return 0;
}

Following is the compilation :

g++ swap.cpp -o swap

Following is the run :

./swap 


Before SWAP-BY-REFERENCE, a = [42], b = [786]
After SWAP-BY-REFERENCE, a = [786], b = [42]


Before SWAP-BY-POINTERS, a = [13], b = [111]
After SWAP-BY-POINTERS, a = [111], b = [13]


Before SWAP-POINTERS, pa = [0x7ffcf0130770], pb = [0x7ffcf0130774]
After SWAP-POINTERS, pa = [0x7ffcf0130774], pb = [0x7ffcf0130770]



Related Solutions

Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
// 5. Predict what the following code will print (write it down), then try it. var...
// 5. Predict what the following code will print (write it down), then try it. var myVariable = "global"; var myOtherVariable = "global"; function myFunction() { var myVariable = "local"; return myVariable; } function myOtherFunction() { myOtherVariable = "local"; return myOtherVariable; javascript
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT