程序,用于查找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
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便