我正在使用 for 循环来搜索数组中存在的最小值并打印该值。我想打印出该值在数组中的位置(0-9),我该怎么做?
int smallest = array[0];
for (counter = 0; counter < 10; counter++) {
if (smallest > array[counter]) {
smallest = array[counter];
}
}
printf("The smallest value stored within the array is %d", smallest);
最佳答案
您只需要另一个变量(初始化为“0”),每次 if 条件为真时,在其中存储“counter”的值,如下所示:
int smallest = array[0];
int position = 0;
for (counter = 0; counter < 10; counter++) {
if (smallest > array[counter]) {
smallest = array[counter];
position = counter;
}
}
printf("The smallest value stored within the array is %d and position = %d", smallest, position);
关于c - 如何在C中打印数组中值的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822959/