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...
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...
Write a C++ or Java code that does similar swap action: def swap (Lst, indexA, indexB)...
Write a C++ or Java code that does similar swap action: def swap (Lst, indexA, indexB) :     t = Lst[indexA]     Lst[indexA] = Lst[indexB]     Lst[indexB] = t L = [1,2,3,4,5,6,7,8,9,10] i=3 j=7 print(L[i], L[j]) swap(L,i,j) print(L[i],L[j])
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...
// 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
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...
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...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print the address of x (using the address-of operator). Pass x as an argument to a function void fooA(int* iptr). (Hint: can you pass x itself directly?) In fooA(int* iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself. In the main function, following the call to fooA(...) , print the value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT