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.


Expert's answer

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!

LATEST TUTORIALS
APPROVED BY CLIENTS