【发布时间】:2021-12-23 08:23:39
【问题描述】:
int smallest = array[0];
int pos = 0;
for (int x = 0; x < 10; x++) {
if (array[x] < smallest) smallest = array[x];
pos = x;
}
printf ("%d is the smallest number and its located in %d\n", smallest, pos);
printf ("\n");
int largest = array[10];
int position = 0;
for (int y = 0; y < 10; y++) {
if (array[y] > largest) largest = array[y];
position = y;
}
printf ("%d is the largest number and its located in %d\n", largest, position);
我在这里尝试的方法不起作用,当我编译并运行它时,它每次都会给我最大和最小数组的位置 9。
【问题讨论】:
-
提供最小的完整程序。这个赋值 int large = array[10];调用未定义的行为。