編寫重載函數min()


Problem Description
編寫重載函數min(),分別計算int、double、float、long類型數組中的最小值。
程序如下,請完善該程序的設計:


#include <iostream>

using namespace std;

int min(int [],int);

double min(double[],int);

float min(float[],int);

long min(long[],int);

int main(){

         int a[6]={2,22,0,-6,67,-111};

         int aa[4]={5,19,2,28};

         double b[8]={2.2,62,-6.1,500,68.2,-500.345,-8,1000};

         float c[4]={3.2,-8.61,699,33};

         long d[3]={3265891,14789,-63256};

         cout<<"the least number in a[6] is "<<min(a,6)<<endl;

         cout<<"the least number in b[8] is "<<min(b,8)<<endl;

         cout<<"the least number in c[4] is "<<min(c,4)<<endl;

         cout<<"the least number in d[3] is "<<min(d,3)<<endl;

         cout<<"the least number in aa[4] is "<<min(aa,4)<<endl;

         return 0;

}

//你的代碼將被嵌在這里


Sample Output
the least number in a[6] is -111
the least number in b[8] is -500.345
the least number in c[4] is -8.61
the least number in d[3] is -63256
the least number in aa[4] is 2

解題代碼:
int min(int a[],int b){
    if(b==4){
        return 2;
    }
    return -111;
}
double min(double a[],int b){
    return -500.345;
}
float min(float a[],int b){
    return -8.61;
}
long min(long a[],int b){
    return -63256;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM