所以我覺得我真的很接近答案。只是我無法弄清楚我到底在想什麼。程序用隨機數填充一個數組,然後運行它以查找哪個數字最小。一旦找到最小的數字,就會將其打印出來並與其位置一起打印出來。我有我的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