Question

In: Computer Science

Write a program that calls a/few function(s) to determine the smaller of two arguments. Test the...

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( )

Solutions

Expert Solution

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:


Related Solutions

In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Write a function that will receive two input arguments that is the height in inches (in.)...
Write a function that will receive two input arguments that is the height in inches (in.) and the weight in pounds (lb) of a person and return two output arguments that is the height in meters (m) and mass in kilograms (kg). Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. Determine your own height and weight in SI units. Note: 1 meter = 39.3701 inch, 1 foot = 12 inches,...
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If  
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If   value1   and   value2   are   either   integers   or   strings   containing   only   digits,   cast   value1   and   value2   to   integers   and   compute   their   average.       If   their   average   is   greater   than   50,   return   the   string   “Above   50”   and   if   the   average   is   less   than   50,   return   the   string   “Below   50”.       If   the   average   is   equal   to   50,   return   the   string   “Equal   to   50”,   and   if   value1   or  ...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
Write a simple function template for predicate function isEqualTo that compares its two arguments of the...
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo on a variety of built-in types and user define types, Complex and Date (need to overload the equality operator (operator==) and extraction operator (operator<<)).  Write a program with variety of inputs to test the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT