2019-11-26 10:55:12 +01:00
|
|
|
/*
|
|
|
|
* Function Overloading
|
|
|
|
*
|
|
|
|
* generalisation of type is possible
|
|
|
|
* => not writing an int implementation means always use the double
|
2020-07-01 14:11:04 +02:00
|
|
|
* => => even when two ints are supplied, they are generalised to doubles
|
2019-11-26 10:55:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-07-01 14:11:04 +02:00
|
|
|
int min( int a, int b );
|
|
|
|
double min( double a, int b );
|
|
|
|
double min( int a, double b );
|
2019-11-26 10:55:12 +01:00
|
|
|
double min( double a, double b );
|
|
|
|
|
|
|
|
int min( int a[], int size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
const int size = 5;
|
|
|
|
int list[size] = { 42, 37, 29, 23, 19 };
|
|
|
|
|
|
|
|
std::cout << "min(5, 1) = " << min(5,1) << std::endl;
|
|
|
|
std::cout << "min([42,37,29,23,19],5) = " << min(list,size) << std::endl;
|
|
|
|
|
|
|
|
std::cout << "min( 3.12345, 3.15432 ) = " << min(3.12345, 3.15432) << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "min( 3.12345, 4 ) = " << min(3.12345, 4) << std::endl;
|
2020-07-01 14:11:04 +02:00
|
|
|
std::cout << "min( 4, 3.12345 ) = " << min(4, 3.12345) << std::endl;
|
2019-11-26 10:55:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:11:04 +02:00
|
|
|
int min( int a, int b ) {
|
|
|
|
if ( a < b ) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
double min( double a, int b ) {
|
|
|
|
if ( a < b ) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
double min( int a, double b ) {
|
|
|
|
if ( a < b ) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
2019-11-26 10:55:12 +01:00
|
|
|
|
|
|
|
double min( double a, double b ) {
|
|
|
|
if ( a < b ) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int min( int a[], int size ) {
|
|
|
|
int m = a[0];
|
|
|
|
|
|
|
|
for( int i = 1; i < size ; ++i ) {
|
|
|
|
m = min(a[i], m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
2020-07-01 14:11:04 +02:00
|
|
|
|
|
|
|
// Fix interchangeabiliy of doubles and ints
|