Write a program with a function template for a function that will determine the
ID: 3541865 • 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.
Thats what I have so far, I know most of it is wrong but I am not sure how to fix it:
#include <iostream>
using namespace std;
template <class T>
T max(T);
void main()
{
int x, y, z;
float r, s, t;
cout << "Enter 3 integers: ";
cin >> x >> y >> z;
cout << "max of the integers is: " << max(x, y, z) << endl;
cout << "Enter 3 numbers as a float: ";
cin >> r, s, t;
cout << "The max of the floats is: " << max(r, s, t) << endl;
}
template <class T>
T max(T a, T b, T c)
{
if (a > b && a > c)
return a;
esle if (b > a && b > c)
return b;
else
return c;
}
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.