所以我觉得我真的很接近答案。只是我无法弄清楚我到底在想什么。程序用随机数填充一个数组,然后运行它以查找哪个数字最小。一旦找到最小的数字,就会将其打印出来并与其位置一起打印出来。我有我的for循环找到最小的整数的麻烦。找到最小整数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(int argc, char* argv[])
{
const int len = 8;
int a[len];
int smallest;
int location =1;
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");
//Find the smallest integer
smallest = a[0];
for (i = 1; i < len; i++)
{
if (a[i] < smallest)
{
smallest = a[i];
location = i++;
}
printf("The smallest integer is %d at position %d\n", smallest, location);
getchar();
}
}
注意:'int location = 1;'应该是'int location = 0;'else'a [0]'不能是最小的。 – chux 2014-09-26 02:45:46