그래서 저는 메신저가 정말 답답한 느낌입니다. 그냥 내가 무엇을 놓치고 있는지 정확히 파악할 수 없습니다. 이 프로그램은 배열에 임의의 숫자를 채운 다음 실행하여 가장 작은 숫자를 찾습니다. 가장 작은 번호를 찾으면 위치와 함께 인쇄합니다. 가장 작은 정수를 찾으려면 내 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