c++ Write templates for three functions: minimum, maximum, and divide. The minim
ID: 3595082 • Letter: C
Question
c++
Write templates for three functions: minimum, maximum, and divide.
The minimum function should accept two arguments and return the value of the argument that is the
lesser of the two.
The maximum function should accept two arguments and return the value of the argument
that is the greater of the two.
The divide function should accept two arguments and divide one by the other.
If the denominator is zero, the divide function should throw an exception. Catch the exception in main().
This is what I have so far
#include<iostream>
using namespace std;
template <class T>
T Maximum(T arg1, T arg2)
{
if(arg1>arg2)
return arg1;
else
return arg2;
}
template <class T>
T Minimum(T arg1, T arg2)
{
if(arg1>arg2)
return arg2;
else
return arg1;
}
template <class T>
T Divide(T arg1, T arg2)
{
arg1 / arg2;
}
int main()
{
int num1, num2;
int big, little, division;
cout << "Please enter the first number : ";
cin >> num1;
cout << "Please enter the second number : ";
cin >> num2;
big = Maximum(num1, num2);
little = Minimum(num1, num2);
division = Divide(num1, num2);
cout << "Largest of two is : " << large<< endl;
cout << "Smallest of the two is : " << small << endl;
cout << "The division of the two numbers is : " << division << endl;
return 0;
}
I am unsure how to make a template for the division.
Explanation / Answer
#include<iostream>
using namespace std;
template <class T>
T Maximum(T arg1, T arg2)
{
if(arg1>arg2)
return arg1;
else
return arg2;
}
template <class T>
T Minimum(T arg1, T arg2)
{
if(arg1>arg2)
return arg2;
else
return arg1;
}
template <class T>
T Divide(T arg1, T arg2)
{
if(arg2 == 0){
throw "Division by zero condition! ";
}
return (arg1 / arg2);
}
int main()
{
int num1, num2;
int big, little, division;
cout << "Please enter the first number : ";
cin >> num1;
cout << "Please enter the second number : ";
cin >> num2;
big = Maximum(num1, num2);
little = Minimum(num1, num2);
try{
division = Divide(num1, num2);
}catch(const char* msg){
cerr<<msg<<endl;
}
cout << "Largest of two is : " << big<< endl;
cout << "Smallest of the two is : " << little << endl;
cout << "The division of the two numbers is : " << division << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.