Answer to Question #224819 in C++ for waleed

Question #224819

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.


1
Expert's answer
2021-08-10T11:51:57-0400
#include<iostream>
using namespace std;
template <typename P>
P Max(P a, P b, P c){
	P  max = a;
	if (b > a && b > c){
		max = b;
	}
	else if(c>a && c>b){
	max = c;
	}
	return max;
}
int main(){
	int a = 6, b=8, c=2;
	char first = 'a', second = 'b', third='d';
	float beginner = 12.8849, guru = 9.09283, coder = 8.98893;
	cout<<"The greatest integer is:  "<<Max(a,b,c)<<endl; //Testing generic function with integers
	cout<<"The maximum character is:  "<<Max(first,second, third)<<endl;//Testing generic function with charactares
	cout<<"The maximum float is:   "<<Max(beginner, guru, coder);////Testing generic function with float numbers
}

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