C++ Programming Write a program with a function template for a function that wil
ID: 3745847 • Letter: C
Question
C++ Programming
Write a program with a function template for a function that will determine the maximum value of three arguments. Write a program where the user will enter three integers and use the function to find the largest. Then the user will enter three floats and use the function to find the largest. Then the user will enter three characters and use the function to find the largest. Include the following function prototype of the function template, where T is the any type: template T max(T, T, T); // 3 args of type T, return type same type T
Explanation / Answer
#include <iostream>
using namespace std;
template<class T>
T greatestNum(T x, T y, T z)
{
T big;
big = x > y ? (x > z ? x : z) : (y > z ? y : z) ;
return big;
}
int main()
{
int choice;
int a,b,c;
float x,y,z;
char p,q,r;
char option;
do
{
cout<<"Largest of three numbers using function template"<<endl;
cout<<"1. Among integer numbers"<<endl;
cout<<"2. Among float numbers"<<endl;
cout<<"3. Among double numbers"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter three Integer numbers"<<endl;
cin>>a>>b>>c;
cout<<"The greatest integer is "<<greatestNum(a,b,c)<<endl;
break;
case 2:
cout<<"Enter three floating numbers"<<endl;
cin>>x>>y>>z;
cout<<"The greatest float number is "<<greatestNum(x,y,z)<<endl;
break;
case 3:
cout<<"Enter three characters numbers"<<endl;
cin>>p>>q>>r;
cout<<"The greatest character is "<<greatestNum(p,q,r)<<endl;
break;
}
cout<<"Do you want to continue (y/n)"<<endl;
cin>>option;
}
while(option=='y' || option=='Y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.