Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Just need 1-3 answered all 50% 10:12 PM /jsums.blackboard.com: Assignment: Chapt

ID: 3763915 • Letter: J

Question

Just need 1-3 answered


all 50% 10:12 PM /jsums.blackboard.com: Assignment: Chapter 16 Programmlng Warmups Programming Warm-Lp Exercises 1. Wrine a function semplate fear vnid fanctinn that squares and srputs he valu pasd 2Write tatcmens that call the furction in Excrce 1 far valuc af typc int, lang, and Write a nececialirarion ofthe function in Exccise 1 thaacceps a string argumenm 4 We would like to have a value secrzing funcion tbat cervans twice the value of its to itx parsmetes The typd the parameier is the cemplate type 6063515 Cornel and "sqaarnes it by pninting ir rwice with no interveningepaces ooe wish on Int parametec and one wirk aoparametes b Weite callag code ther akes calls to both functions msider the GList class template of Section 16.1 Write client code to isstanciate the template twice, reating a list f ints and ali of floats. Assume that at some point in the client code the list of ints already contains valu known to be in the range 10 through 80 and the list of floats is cmpty. Write clic code that empties the list of ints as follows: As each item is removed from theli of ints, malhiply it by 0.5 and insert the result into the list o floats rite a MixedPair dass template that is simalar to the OrdPair template of Quick Che ueatsom 16.1.2 excepe that the pair af items an be od two different data types. Ha

Explanation / Answer

1.

#include <type_traits>

#include <iostream>

template <typename T>

T foo(T && t,

    typename std::enable_if<std::is_same<T,int>::value, void **>::type = nullptr)

{

    std::cout << "Doubling " << t << " gives " << (t + t) << std::endl;

    return t + t;

}

template <typename T>

T foo(T && t, typename std::enable_if<!std::is_same<T,int>::value, void **>::type = nullptr)

{

    std::cout << "Squaring " << t << " gives " << (t * t) << std::endl;

    return t * t;

}

using namespace std;

int main()

{

    cout << foo(2) << endl;

    cout << foo(3.3) << endl;

    return 0;

}

2.

#include <iostream>

using namespace std;

template <class T>

inline T square(T x)

{

   T result;

   result = x * x;

   return result;

};

main()

{

   int    i, ii;

   float x, xx;

   double y, yy;

   i = 2;

   x = 2.2;

   y = 2.2;

   ii = square<int>(i);

   cout << i << ": " << ii << endl;

   xx = square<float>(x);

   cout << x << ": " << xx << endl;

   // Explicit use of template

yy = square<double>(y);

   cout << y << ": " << yy << endl;

   // Implicit use of template

   yy = square(y);

   cout << y << ": " << yy << endl;

}