Answer to Question #224676 in C++ for Umar

Question #224676

Create a generic function max that gives the maximum value of three generic type arguments that are passed to it. Then test this function by calling it with char, int, and float type. (Please provide executeable code in C++).


1
Expert's answer
2021-08-10T04:28:00-0400
template<typename T>
T max(T v1, T v2, T v3)
{
    T m = v1 >= v2 ? v1 : v2;
    return m >= v3 ? m : v3;
}

int main()
{
    char  maxChar  = max('b', 'c', 'a');
    int   maxInt   = max(2, 3, 1);
    float maxFloat = max(2.0f, 3.0f, 1.0f);
    
    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
APPROVED BY CLIENTS