在数组中找到最小值是一个经典的C语言数组程序。 该程序可以让您了解迭代,数组和条件运算符。 我们迭代地检查数组的每个元素并找到最小值。 参见下面的程序。
实现
实现计算数组元素的最小值的代码如下 -
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int loop, smallest;
smallest = array[0];
for(loop = 1; loop < 10; loop++) {
if( smallest > array[loop] )
smallest = array[loop];
}
printf("Smallest element of array is %d", smallest);
return 0;
}
执行上面示例代码,得到以下结果 -
Smallest element of array is 1