Answer to Question #202804 in C++ for Adarsh singh

Question #202804

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-04T06:02:53-0400
#include<iostream>
using namespace std;
template <typename P>
P max(P x, P y , P z){
	P value =  x;
	if(y>x && y>z){
		value = y;
	}
	else if(z>x && z> y){
		value = z;
	}
	return value;
}
int main(){
//	Showing the ability of template function working the integer values
	int x, y, z;
	double q, r, t;
	char k, l, m;
cout<<"Showing the ability of template function working the integer values"<<endl;
cout<<"First Integer Value"<<endl;
cin>>x;
cout<<"Second Integer Value"<<endl;
cin>>y;
cout<<"Third Integer value"<<endl;
cin>>z;
int integer_value = max<int>(x,y,z);
cout<<"maximum is:\t "<<integer_value <<endl;
//Showing the ability of template function working the double values


cout<<"Showing the ability of template function working the double values"<<endl;
cout<<"First value of type double"<<endl;
cin>>q;
cout<<"Second value of type double "<<endl;
cin>>r;
cout<<"Third value of type double"<<endl;
cin>>t;
double double_value = max<double>(q,r,t);
cout<<"Maximum :\t "<<double_value<<endl;
//Showing the ability of template function working the double values
	
cout<<"Template function working the char values"<<endl;
cout<<"First char value"<<endl;
cin>>k;
cout<<"Second char value"<<endl;
cin>>l;
cout<<"Third char value"<<endl;
cin>>m;
char char_value = max<char>(k,l,m);
cout<<"Maximum \t "<<char_value<<endl;


}

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