56 lines
991 B
C++
56 lines
991 B
C++
|
/*
|
||
|
* Function Overloading
|
||
|
*
|
||
|
* generalisation of type is possible
|
||
|
* => not writing an int implementation means always use the double
|
||
|
* => => even when to ints are supplied, they are generalised to doubles
|
||
|
*/
|
||
|
|
||
|
#include <iostream>
|
||
|
|
||
|
//int min( int a, int b );
|
||
|
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;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
//int min( int a, int b ) {
|
||
|
// if ( a < b ) {
|
||
|
// return a;
|
||
|
// }
|
||
|
// return b;
|
||
|
//}
|
||
|
|
||
|
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;
|
||
|
}
|