Problem B: 编写重载函数min()

发布于:2022-12-09 ⋅ 阅读:(252) ⋅ 点赞:(0)
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
<span style="background-color:#ffffff"><span style="color:#333333"><span style="color:#333333"><span style="background-color:#f4fbff">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</span></span></span></span>

int min(int a[], int n)
{
	int min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (min > a[i])
			min = a[i];
	}
	return min;
}
double min(double a[], int n)
{
	double min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (min > a[i])
			min = a[i];
	}
	return min;
}
float min(float a[], int n)
{
	float min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (min > a[i])
			min = a[i];
	}
	return min;
}

long min(long a[], int n)
{
	long min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (min > a[i])
			min = a[i];
	}
	return min;
}

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到