Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何找到数组中最大值和最小值的位置?_C_Arrays_Position_Max_Min - Fatal编程技术网

C 如何找到数组中最大值和最小值的位置?

C 如何找到数组中最大值和最小值的位置?,c,arrays,position,max,min,C,Arrays,Position,Max,Min,程序将最大值的位置打印为3。 它将最小值的位置打印为4。 但最小位置的正确答案应该是2和4。 在我的例子中,最大值是4,所以位置应该从0算起3。 最小值为1,因此位置应为2和4。 现在,我的代码只显示最小值的一个位置:4。 它应该打印两个位置:2和4 我想打印出最大值和最小值的所有位置 这实际上是一个很好的问题。解决方案的关键是在数组A中找到最小值和最大值。这部分已经完成,并且运行良好。这里唯一的改进是从索引1开始,并从>=中删除=这是为什么?代码正在查找上次出现的最大值和最小值。最大值为4,最

程序将最大值的位置打印为3。 它将最小值的位置打印为4。 但最小位置的正确答案应该是2和4。 在我的例子中,最大值是4,所以位置应该从0算起3。 最小值为1,因此位置应为2和4。 现在,我的代码只显示最小值的一个位置:4。 它应该打印两个位置:2和4


我想打印出最大值和最小值的所有位置

这实际上是一个很好的问题。解决方案的关键是在数组A中找到最小值和最大值。这部分已经完成,并且运行良好。这里唯一的改进是从索引1开始,并从>=中删除=这是为什么?代码正在查找上次出现的最大值和最小值。最大值为4,最小值为1。最后一次出现在索引4min和索引3max处。您预计第一次出现吗?然后尝试>而不是>=并且我想打印出最大和最小数字的所有位置。在这种情况下,最大数字是4,因此位置应该是从0算起的3。最小值为1,因此位置应为2和4。现在我的代码只显示最小编号位置为4。我想要的是,它显示了2和4,这确实有助于理解你的问题。我建议在你的问题中解释一下。习惯于这样做。这也有助于避免松散和densley两个词带来的混淆。或者通过避免使用这些词,或者通过解释它们。基本上,您希望输出关于最大索引的多个信息和关于最小索引的多个信息。您是否考虑过使用多个变量来存储这些变量?对于这两种情况,您可能需要使用尽可能多的变量,因为所有数组成员都可以包含最大值和最小值。谢谢。那真是帮了大忙
int A[5] = {2, 2, 1, 4, 1};    
int max = A[0], min = A[0];
int k,l;

for (i = 0; i < 5; i++) {
    if (A[i] >= max) {
        max = A[i];
        k = i;
    }
}

for (j = 0; j < 5; j++) {
    if (A[j] <= min) {
        min = A[j];
        l = j;
    }
}

printf("densely is: %d ", k);
printf("\n");
printf("loosely is: %d ", l);
#include <stdio.h>

// Find all minimums and all maximums in the array A
// Find and remember where `max` and `min` values were in the array A
// The algorithm is symmetrical for minimum and maximum 

int main(void)
{             // 0  1  2  3  4 
    int A[5] = { 2, 2, 1, 4, 1};    

    int maximums[5] = {0};  // we keep max positions here
    int minimums[5] = {0};  // we keep min positions here

    int max = A[0];  // we assume max at index 0
    int min = A[0];  // we assume min at index 0

    for (int i = 1; i < 5; i++) {       // we can start from index 1 because of the above assumption
        if (A[i] > max) max = A[i];     // densely 
        if (A[i] < min) min = A[i];     // loosely
    }

    // Find the all elements equal to `max` and `min` 
    // and register them in `maximums` and `minimums` arrays

    // Print the results:
    printf("max value is: %d and it occurs at the following index(es):\n", max);         
    for (int i = 0; i < 5; i++) {

        if (A[i] == max){ 
            maximums[i] = 1;   // mark this index as maximum being here
            printf("%d ", i);
        }
    }

    printf("\nmin value is: %d and it occurs at the following index(es):\n", min );   
    for (int i = 0; i < 5; i++) {

        if (A[i] == min){ 
            minimums[i] = 1;   // mark this index as minimum being here
            printf("%d ", i);
        }    
    }

    // At this moment we not only printed the results 
    // but the array minimums and maximums remember where the `min` and `max` were.

    return 0;
}
max value is: 4 and it occurs at the following index(es):                                                                                       
3                                                                                                                                               
min value is: 1 and it occurs at the following index(es):                                                                                       
2 4