Exercise 2: Swapping Values
Let's generalize the function of swapping two values by using templates.
We'll cover the following
Problem Statement
In the code widget below, two functions, both called, swap_values
are declared. Oneswaps values for int
type inputs and the other for double
type.
In this exercise, you need to define a Template Class type function index_of_smallest
that will generalize the function such that it finds the minimum value index for both int
and double
type array input values. This function takes two values passed by reference and swaps them.
Your function should swap the two int
values and the two double
values.
Note: Remove both the
int
anddouble
typeindex_of_smallest
functions and then write the code for the Template Class typeswap_values
function there.
Sample input
Create a free account to access the full course.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy