分别编写求数组a中最大值max(int a[],int n)和最小值min(int a[],int n)的函数,其中n是数组a的大小

2024-11-08 20:01:46
推荐回答(3个)
回答(1):

#include
using namespace std;
int max(int a[], int n)
{
int tmp = a[0];
for (int i = 0; i!=n; ++i)
if (tmp < a[i])
tmp = a[i];
return tmp;
}
int min(int a[], int n)
{
int tmp = a[0];
for (int i = 0; i!=n; ++i)
if (tmp > a[i])
tmp = a[i];
return tmp;
}
int main()
{
int *num = 0, len=0;
cout << "请输入数的个数:";
cin >> len;
num = new int[len];
cout << "请输入所有的数:";
for (int i = 0; i!=len; ++i)
cin >> num[i];
cout << "最大值为" << max(num, len) << endl;
cout << "最小值为" << min(num, len) << endl;
delete [] num;
return 0;
}

回答(2):

#include
using namespace std;

const int SIZE = 10;
int max(int* a, int size);
int min(int* a, int size);

int
main(){
int* a = new int [SIZE];
int Num = 0;
int MAX = 0;
int MIN = 0;

cout<<"Plz input the elements:";
for(int i = 0; cin>>a[i]; i++){
Num++;
}

MAX = max(a, Num);
MIN = min(a, Num);

cout<<"The max number in the array is: "< cout<<"The min number in the array is: "<
delete [] a;

system("pause");
return 0;
}

int
max(int* a, int size){
int result = a[0];
for(int i = 1; i < size; i++){
if(result < a[i]){
result = a[i];
}
}
return result;
}

int
min(int* a, int size){
int result = a[0];
for(int i = 1; i < size; i++){
if(result > a[i]){
result = a[i];
}
}
return result;
}

回答(3):

给100分,帮你做。。