Answer to Question #202822 in C++ for Myname

Question #202822

Write a template function that returns the maximum value of three elements

passed to the function. Parameters passed to the function can be of int, double,

char but all parameters passed are of the same type at point of time. Parameters

to be passed to the function as are read as input.


1
Expert's answer
2021-06-03T18:42:46-0400
template<class T>
T Max(T x1, T x2, T x3) {
    const T max = x1 > x2? x1 : x2;
    return max > x3? max : x3;
}

int main() {
    {
        std::cout << "Enter <integer> a, b, c\n";
        int a, b, c;
        std::cin >> a >> b >> c;
        std::cout << "Max: " << Max(a, b, c) << '\n';
    }
    {
        std::cout << "Enter <double> a, b, c\n";
        double a, b, c;
        std::cin >> a >> b >> c;
        std::cout << "Max: " << Max(a, b, c) << '\n';
    }
    {
        std::cout << "Enter <char> a, b, c\n";
        double a, b, c;
        std::cin >> a >> b >> c;
        std::cout << "Max: " << Max(a, b, c) << '\n';
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog