In: Computer Science
Write a program that calls a/few function(s) to determine the
smaller of two
arguments. Test the program using integer, character and
floating-point number
arguments. Produce 3 separate programs solving the problem above.
The three
approaches you should be using are:
a) 3 different functions (findMinimum1(), findMinimum2(),
findMinimum3()), each to solve different data types
parameters.
b) Function overloading with only one function named findMinimum(
)
c) Function template with only one function named findMinimum(
)
To find mininum of 2 numbers, we have used 3 different ways . By defining function with 2 arguments and returning value. Below code is written in C++ language
1. Three different functions ( findMinimum1(), findMinimum2(), findMinimum3())
findMinimum1() - accepts integer arguments and return smallest integer value
findMinimum2() - accepts character arguments and return smallest character value
findMinimum3() - accepts float arguments and return smallest float value
#include <iostream>
using namespace std;
int findMinimum1(int num1,int num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
char findMinimum2(char char1,char char2){
if(char1>char2){
return char2;
}
else{
return char1;
}
}
float findMinimum3(float num1,float num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
int main() {
int n1,n2;
char c1,c2;
float f1,f2;
cout <<"Enter 2 integers values: ";
cin>>n1>>n2;
cout<<"The minimum of two values is :
"<<findMinimum1(n1,n2)<<"\n";
cout <<"Enter 2 character values: ";
cin>>c1>>c2;
cout<<"The minimum of two values is :
"<<findMinimum2(c1,c2)<<"\n";
cout <<"Enter 2 float values: ";
cin>>f1>>f2;
cout<<"The minimum of two values is :
"<<findMinimum3(f1,f2)<<"\n";
return 0;
}
OUTPUT:
Enter 2 integers values: 2 3
The minimum of two values is : 2
Enter 2 character values: a A
The minimum of two values is : A
Enter 2 float values: 2.3 4.5
The minimum of two values is : 2.3
___________________________________
Please find attached screenshot of code for indentation:
2. Function Overloading:
Function overloading is a concept where two or more function have same name but arguments with different datatype or number of srguments are different
findMinimum() - is overloaded 2 times with same number of arguments but with different datatypes and different return type
#include <iostream>
using namespace std;
int findMinimum(int num1,int num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
char findMinimum(char num1,char num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
float findMinimum(float num1,float num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
int main() {
int n1,n2;
char c1,c2;
float f1,f2;
cout <<"Enter 2 integers values: ";
cin>>n1>>n2;
cout<<"The minimum of two values is :
"<<findMinimum(n1,n2)<<"\n";
cout <<"Enter 2 character values: ";
cin>>c1>>c2;
cout<<"The minimum of two values is :
"<<findMinimum(c1,c2)<<"\n";
cout <<"Enter 2 float values: ";
cin>>f1>>f2;
cout<<"The minimum of two values is :
"<<findMinimum(f1,f2)<<"\n";
return 0;
}
OUTPUT:
Enter 2 integers values: 2 3
The minimum of two values is : 2
Enter 2 character values: a A
The minimum of two values is : A
Enter 2 float values: 2.3 4.5
The minimum of two values is : 2.3
_________________________________________________
Please find attached screenshot of code for indentation:
3. Function Template:
Function template are used to make functions generic . i.e , which is not dependent on dataype for doing calculation
findMinimum() - Generic Template function which can be used to check smallest value with any datatype arguments
#include <iostream>
using namespace std;
template <class T>
T findMinimum(T num1,T num2){
if(num1>num2){
return num2;
}
else{
return num1;
}
}
int main() {
int n1,n2;
char c1,c2;
float f1,f2;
cout << "Enter 2 integers values: ";
cin>>n1>>n2;
cout<<"The minimum of two values is :
"<<findMinimum<int>(n1,n2)<<"\n";
cout << "Enter 2 character values: ";
cin>>c1>>c2;
cout<<"The minimum of two values is :
"<<findMinimum<char>(c1,c2)<<"\n";
cout << "Enter 2 float values: ";
cin>>f1>>f2;
cout<<"The minimum of two values is :
"<<findMinimum<float>(f1,f2)<<"\n";
return 0;
}
OUTPUT:
Enter 2 integers values: 2 3
The minimum of two values is : 2
Enter 2 character values: a A
The minimum of two values is : A
Enter 2 float values: 2.3 4.5
The minimum of two values is : 2.3
Please find attached screenshot of code for indentation: