实现一个函数,函数完成如下功能

实现一个函数,函数完成如下功能:

1.函数的输入为一个数组,数组的成员个数不定(即:可能为 0 个,也可能为多个)。
2.函数找到成员的最大元素和最小元素,并能让函数的调用者知道最大元素和最小元素是哪一个。

#include <stdio.h>
#include <iostream>
#include <Windows.h>

using namespace std;

bool find_max_min(int a[], int len, int* max, int* min) {

	if (len < 1) return false;

	*max = a[0];
	*min = a[0];

	for (int i = 1; i < len; i++) {
		if (*max < a[i]) {
			*max = a[i];
		}

		if (*min > a[i]) {
			*min = a[i];
		}
	}

	return true;
}

int main(void) {
	int a[10] = { 2, 9, 5, 7, 10, 15, 13, 8, 6, 3 };
	int max = 0, min = 0;

	find_max_min(a, 10, &max, &min);

	cout << "max:" << max << ", min:" << min << endl;

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ZengYong12138/article/details/106766220