我四处搜寻,但没有找到任何与我的问题相关的信息。
我想用 C 创建一个算法,查找数组中可容纳 10 个值的最小数字。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void) {
int array[10];
int i, smaller;
srand(time(NULL));
for (i = 0; i < 10; i++) {
array[i] = rand() % 100 + 1;
}
// Find smaller number in the array
for (i = 0; i < 10; i++) {
...
}
printf("Smaller: %d\n", smaller);
return 0;
}
有什么关于我该怎么做的提示吗?
最佳答案
将数组的第一个元素分配给较小的
smaller = array[0];
使用i = 1
开始循环,并将元素与更小的
进行比较。如果 smaller
大于任何 array
元素,则将其替换为该元素,否则保留原样。
关于c - C 中查找数组中最小数字的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361224/