【发布时间】:2014-04-06 18:24:19
【问题描述】:
好的,所以我的程序假设创建一个数组大小 [8],然后一旦打印出来,我就使用 For 循环来查找数组中的最小数字。我遇到的问题是它似乎总是停在第二个元素上并将其声明为最小。谁能告诉我我的代码有什么问题
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(int argc, char* argv[])
{
const int len = 8;
int a[len];
int i;
srand(time(0));
//Fill the array
for(i = 0; i < len; ++i) {
a[i] = rand() % 100;
}
//Print the array
for (i = 0; i < len; ++i) {
printf("%d ", a[i]);
}
printf("\n");
getchar();
int smallest;
for (i = 1; i < len; i++) {
if (a[i] < smallest)
smallest = a[i];
{
printf("The smallest integer is %d at position %d\n", a[i], i);
break;
getchar();
}
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow。
c#和c++标签看起来无关紧要。请阅读FAQ、How to Ask 和help center 作为开始..