Write a program with a function template for a function that will determine the
ID: 3564254 • Letter: W
Question
Write a program with a function template for a function that will determine the maximum value of three arguments. Write a program where the user will enter three integers and use the function to find the largest. Then the user will enter three floats and use the function to find the largest. Then the user will enter three characters and use the function to find the largest. Include the following function prototype of the function template, where T is the any type: template T max(T, T, T); // 3 args of type T, return type same type T
Explanation / Answer
#include <iostream>
using namespace std;
template <class T>
T max(T a, T b, T c);
int main()
{
int x, y, z;
float r, s, t;
char a,b,c;
cout << "Enter 3 integers: ";
cin >> x >> y >> z;
cout << "max of the integers is: " << max(x, y, z) << endl;
cout << "Enter 3 float numbers: ";
cin >> r >> s >> t;
cout << "The max of the floats is: " << max(r, s, t) << endl;
cout << "Enter 3 characters ";
cin >> a >> b >> c;
cout << "The max of the characters is: " << max(a, b, c) << endl;
return 0;
}
template <class T>
T max(T a, T b, T c)
{
if (a > b && a > c)
return a;
else if (b > a && b > c)
return b;
else
return c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.