1. (10 scores)#
点击查看考点
C 风格数组, 递归函数
The following program implements finding the smallest number, the output is as following, please complement it.
翻译
以下程序实现了查找最小值, 输出如下所示, 请补全它.
1Output:
21
1#include <iostream>
2
3using namespace std;
4
5void find(int* a, int n, int i, int& pk) {
6 if (i < /* (1) */) {
7 if (a[i] < /* (2) */) {
8 pk = i;
9 }
10 find(a, n, /* (3) */, pk);
11 }
12}
13
14auto main() -> int {
15 int a[] = {4, 2, 6, 1, 7, 3};
16 int n = 0;
17 find(a, /* (4) */, 0, n);
18 cout << /* (5) */ << endl;
19}
点击查看解答参考
n
, 判断索引是否已经超出数组长度.a[pk]
, 比较当前索引的值与当前最小值之间的大小.i + 1
, 向右继续查找.6
, 传递数组的长度.a[n]
, 输出最小值.