Question

White a function that swaps values using templates int main ( ) { int X=5 int...

White a function that swaps values using templates

int main ( ) {

int X=5

int Y=29 ;

double a = 1.0 ;

double b = 3.14 ;

swap ( X , Y ) ;

swap ( a , b ) ;

return 0 ;

}

I need you to explain it step by step

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

template <typename T>   // make this function templated
void swap(T &n1, T &n2) {   // use T as type of parameters
    T temp = n1;    // set temp as n1
    n1 = n2;    // change n1 to n2
    n2 = temp;  // change n2 to original value of n1 using temp
}

int main() {
    int X = 5;
    int Y = 29;
    double a = 1.0;
    double b = 3.14;
    swap(X, Y);
    swap(a, b);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
White a function that swaps values using templates int main ( ) { int X=5 int...
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
  • Design a swap function with the following interface: void swap( int *x,    int *y) { }...

    Design a swap function with the following interface: void swap( int *x,    int *y) { } In your main( ), you perform the following test: int main( ) {             int a = 10, b = 20;             cout << “a = “ << a << “   b= “   << b   << endl;             swap( a, b);             cout << “a = “ << a << “   b= “   << b   << endl;             return 0;                         … }

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

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

  • Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps...

    Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps X[k] and X[k+1] and returns 1; otherwise, it returns 0. You may use either conditional branch or conditional execution or their combination. int swap(unsigned short X[], int k) if (X[k] > X[k+1]) { short tmp = X[k]; X[k+1] = X[k]; X[k] = tmp; return 1; else { return 0;

  • 1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf...

    1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...

  • #include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij);...

    #include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij); printf("k=%d\n",k); return 0; int function] (int x, int y) int z; z=x*2+y; return z;

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

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

  • What does this function do? int mystery(double employees[], double id, int size) { for (int i...

    What does this function do? int mystery(double employees[], double id, int size) { for (int i = 0; i < size; i++) { if (id == employees[i]) { return i; } } return -1; } A. This is a function that performs a search. If the id is found in the employees array, its index location is returned, otherwise -1 is returned. B. This is a function that sorts the employees array C. This is a function that returns all...

  • C++ What is the result of the following program? #include <algorithm> int main() {int x =...

    C++ What is the result of the following program? #include <algorithm> int main() {int x = 10; int *y = &x; int *z = new int; z[0] = 20; std::swap (y, z); return x;} (A) Does not compile. (B) Runtime Error or Undefined Behavior (C) Returns 10 (D) Returns 20

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
Active Questions
ADVERTISEMENT