【问题标题】:How do I print the index of the maximum and minimum values?如何打印最大值和最小值的索引?
【发布时间】:2020-09-19 22:10:13
【问题描述】:

以下代码打印最大值和最小值。如何打印这些值的索引而不是值本身?

#include <stdio.h>

int main()
{
    int arr1[100];
    int i, mx, mn, n;   

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
          printf("element - %d : ",i);
          scanf("%d",&arr1[i]);
        }


    mx = arr1[0];
    mn = arr1[0];

    for(i=1; i<n; i++)
    {
        if(arr1[i]>mx)
        {
            mx = arr1[i];
        }


        if(arr1[i]<mn)
        {
            mn = arr1[i];
        }
    }
    printf("Maximum element is : %d\n", mx);
    printf("Minimum element is : %d\n\n", mn);

    return 0;
}

【问题讨论】:

  • 只有另外两个变量(比如mximni),并将它们设置为“当前”i 值,与您设置mx 和@987654326 的位置相同@值。
  • 只需再添加两个变量,如 maxIndexminIndex 用于存储值,并将 maxIndex 放在第一个循环中,minIndex 放在第二个 if 循环中,用于存储索引值最大值和最小值,然后使用这些变量。就是这样。

标签: c arrays max min function-definition


【解决方案1】:

更改代码以跟踪最小和最大元素的索引并不难。

只需在变量mxmn 中存储相应的最小和最大元素的索引,而不是例如它们的值。

mx = 0;
mn = 0;

for ( i = 1; i < n; i++ )
{
    if ( arr1[i] > arr1[mx] )
    {
        mx = i;
    }
    else if ( arr1[i] < mn )
    {
        mn = i;
    }
}

但我想指出的是,尝试始终编写更通用的代码。

您可以编写一个单独的函数,返回一对最大和最小元素的索引。

你来了。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct Pair { size_t min; size_t max; } 
minmax_element( const int a[], size_t n )
{
    struct Pair minmax = { .min = 0, .max = 0 };

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < a[minmax.min] )
        {
            minmax.min = i;
        }
        else if ( a[minmax.max] < a[i] )
        {
            minmax.max = i;
        }
    }

    return minmax;
}

int main(void) 
{
    size_t n = 1;

    printf( "Input the number of elements to be stored in the array: " );
    scanf( "%zu", &n );

    int a[n];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < n; i++ )
    {
        a[i] = rand() % ( int )n;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    struct Pair minmax = minmax_element( a, n );

    printf( "The minimum value is %d at position %zu\n", a[minmax.min], minmax.min );
    printf( "The maximum value is %d at position %zu\n", a[minmax.max], minmax.max );

    return 0;
}

程序输出可能看起来像

Input the number of elements to be stored in the array: 10
7 1 7 3 1 7 8 5 0 3 
The minimum value is 0 at position 8
The maximum value is 8 at position 6

或者,该函数可以使用两个附加参数来定义:指向最小元素索引的指针和指向最大元素索引的指针。

你来了。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void minmax_element( const int a[], size_t n, size_t *min, size_t *max )
{
    *min = 0;
    *max = 0;

    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[i] < a[*min] )
        {
            *min = i;
        }
        else if ( a[*max] < a[i] )
        {
            *max = i;
        }
    }
}

int main(void) 
{
    size_t n = 1;

    printf( "Input the number of elements to be stored in the array: " );
    scanf( "%zu", &n );

    int a[n];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < n; i++ )
    {
        a[i] = rand() % ( int )n;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    size_t min, max;

    minmax_element( a, n, &min, &max );

    printf( "The minimum value is %d at position %zu\n", a[min], min );
    printf( "The maximum value is %d at position %zu\n", a[max], max );

    return 0;
}

程序输出可能看起来像

Input the number of elements to be stored in the array: 10
2 0 4 2 4 3 0 9 1 0 
The minimum value is 0 at position 1
The maximum value is 9 at position 7

【讨论】:

    【解决方案2】:

    作为上一个答案的替代方案,您可以使用指针和指针算术:

    #include <stdio.h>
    
    int main()
    {
        int arr1[100];
        int i, n;
        int *mn, *mx;
    
        printf("Input the number of elements to be stored in the array :");
        scanf("%d", &n);
    
        printf("Input %d elements in the array :\n",n);
        for(i = 0; i < n; i++)
        {
            printf("element - %d : ", i);
            scanf("%d", &arr1[i]);
        }
    
        mx = &arr1[0];
        mn = &arr1[0];
    
        for(i = 1; i < n; i++)
        {
            if(arr1[i] > *mx)
                mx = &arr1[i];
    
            if(arr1[i] < *mn)
                mn = &arr1[i];
        }
    
        printf("Maximum element is : %d (%d)\n", *mx, mx - arr1);
        printf("Minimum element is : %d (%d)\n\n", *mn, mn - arr1);
    
        return 0;
    }
    

    例子:

    $ gcc main.c -o main.exe; ./main.exe; 输入要存储在数组中的元素数:5 在数组中输入 5 个元素: 元素 - 0 : 3 元素 - 1 : 2 元素 - 2:1 元素 - 3 : 5 元素 - 4 : 4 最大元素为:5 (3) 最小元素为:1 (2)

    【讨论】:

      【解决方案3】:

      分别更新mxmn时只需保存索引:

      int max_index = 0;
      int min_index = 0;
      
      // ...
      
          if(arr1[i]>mx)
          {
              mx = arr1[i];
              max_index = i;
          }
      
      
          if(arr1[i]<mn)
          {
              mn = arr1[i];
              min_index = i;
          }
      

      【讨论】:

        猜你喜欢
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 2019-05-26
        • 1970-01-01
        • 2021-07-09
        • 2020-09-06
        • 2014-12-27
        • 2020-03-10
        相关资源
        最近更新 更多