程序,用于查找C ++中数组的最小(或最大)元素
为此,我们将提供一个数组。我们的任务是在该数组中找到最大和最小元素。
示例
#include <bits/stdc++.h>
using namespace std;
//寻找最小元素
int getMin(int arr[], int n) {
int res = arr[0];
for (int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
//寻找最大元素
int getMax(int arr[], int n) {
int res = arr[0];
for (int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
int main() {
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum element of array: " << getMin(arr, n) << "\n";
cout << "Maximum element of array: " << getMax(arr, n);
return 0;
}输出结果
Minimum element of array: 1 Maximum element of array: 1234
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短