Exercise 2: Swapping Values

This exercise requires you to use template type to generalize the function used to swap two values

We'll cover the following

Problem Statement

In the code widget below, two functions both called swap_values are declared. One finds swaps values for int type inputs and the other for double type.

In this exercise, you need to define a Template Class type function called swap_values that will generalize the function such that it finds swaps values for both int and double type input values.

IMPORTANT NOTE: Remove both the int and double type swap_values functions and then write the code for the Template Class type swap_values function there.

Your template class type swap_values function code should swap two values.

  • The function takes two values passed by reference and swaps them.

Down below is what the expected output should look like.

Your function will be tested on Inputs

int x1 = 2 , int y1 = 3, double x1 = 2.5 , double y2 = 3.5

Your function should swap the two int values and the two double values.

Here is what the output should look like in this case.

Expected Output:

The expected output will be displayed as a string with the swapped int values appended first and sorted double values appended after.

3 2 3.5 2.5

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy