【问题标题】:Finding minimum and maximum value stored in a user input array in C在C中查找存储在用户输入数组中的最小值和最大值
【发布时间】:2020-07-10 22:30:34
【问题描述】:

我正在尝试用这个程序做两件主要的事情:

  • 让用户输入数字,直到他们输入 0 或直到他们到达数组的末尾。
  • 在第二部分中,我想找到存储在数组中的最小值和最大值并将它们打印出来。

最大值打印准确,但最小值打印看起来像地址的东西。

这是我目前的代码,你能帮帮我吗?

#include<stdio.h>

#define SIZE 100
int main()
{
    int count = 0, store, array[SIZE], total;
    int min = array[0], max = array[0];

    do
    {
        printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
        scanf("%d", &store);
        array[count ++] = store;
    } while ( count < SIZE && store != 0 );

    total = count;
    printf("Total number of values in array is %d.\n", total);

    for ( count = 0 ; count <= total && array[count] != 0 ; count ++)
    {
        if(array[count] < min)
        {
            min = array[count];
        }

        if(array[count] > max)
        {
            max = array[count];
        }
    }

    printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);

    return 0;
}

【问题讨论】:

  • 读取值后移动int min = array[0], max = array[0];。
  • 您错误地初始化了 min 和 max,因为此时您甚至没有读入用户输入,因此它们可能是一些垃圾值。
  • 永远不要在同一行声明/初始化多个变量,这样就不会出现这些错误。
  • @NadavS 怎么样??
  • 你不需要两个循环。您可以在收到值时找到最大值和最小值。

标签: c arrays max user-input min


【解决方案1】:

在数组被填充后移动 min 和 max 变量声明,像这样

#include<stdio.h>

#define SIZE 100
int main()
{
    int count = 0, store, array[SIZE], total;

    do
    {
        printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
        scanf("%d", &store);
        array[count ++] = store;
    } while ( count < SIZE && store != 0 );

    int min = array[0], max = array[0];

    total = count;
    printf("Total number of values in array is %d.\n", total);

    for ( count = 0 ; count <= total && array[count != 0] ; count ++)
    {
        if(array[count] < min)
        {
            min = array[count];
        }

        if(array[count] > max)
        {
            max = array[count];
        }
    }

    printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);

    return 0;
}

然而,如果你写 0 来停止插入数字,那么最小值将始终为 0

【讨论】:

  • 您的回答有两处错误。首先,您复制了 OP 在 for 循环条件下所做的相同输入错误(array[count != 0] 将始终是数组的元素 0 或 1)。其次,如果输入错误的条件正确(即array[count] != 0),则不会将值 0 作为最小值放置,因为 for 循环将退出。
  • @Tudor 代码已经过测试和工作。我没有注意到 array[count != 0] 但它没有任何区别,因为编译器会忽略它。我只是专注于问题,并没有阅读整个代码。
  • @DesislavaIvanova 很高兴我提供了帮助。您能否将答案设置为解决方案?谢谢!
【解决方案2】:

您的代码中有错误。首先,这个初始化是错过了int min = array[0], max = array[0];,当你没有它时,你不能将array[0] 的值分配给其他变量。这个初始化应该在do-while之后进行。(否则就像使用未初始化的变量)

第二个array[count != 0] 这应该是array[count]!=0。

你也应该在扫描arrayif(count==1) return 0之后检查这个语句,这意味着用户没有输入任何输入。

还请注意count&lt;=total 您正在传递 `array 的边界,这将导致未定义的行为。

#define SIZE 100
int main()
{
    int count = 0, store, array[SIZE], total;

    do
    {
        printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
        scanf("%d", &store);
        array[count++] = store;
    } while (count < SIZE && store != 0);
    int min = array[0], max = array[0];
  if(count==1)
 {
  printf("no data");
  return 0;
 }
    total = count;
    printf("Total number of values in array is %d.\n", total);

    for (count = 0; count < total && array[count]!=0; count++)//or use count < total-1 instead of  total && array[count]!=0
    {
        if (array[count] < min)
        {
            min = array[count];
        }

        if (array[count] > max)
        {
            max = array[count];
        }
    }

    printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);

    return 0;
}

【讨论】:

    【解决方案3】:

    您的代码的问题是(我认为您打错了)for 循环中的第二个条件。如果您测试array [count != 0],表达式count != 0 的值可以是1 或0,那么您只需访问array[0] 或array[1]。

    如果您想更正它,只需将其替换为 array[count] != 0 但这不是好的解决方案。您已经计算了需要检查多少个数字,为​​什么不忽略最后一个(即 0)?

    这样做,这就是你想要的代码:

    #define SIZE 100
    int main()
    {
        int count = 0, store, array[SIZE], total;
    
        do
        {
            printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
            scanf("%d", &store);
            array[count ++] = store;
        } while ( count < SIZE && store != 0 );
    
        int min = array[0], max = array[0];
    
        total = count;
        printf("Total number of values in array is %d.\n", total);
    
        for (count = 0; count < total-(total != SIZE); count ++)
        {
            if(array[count] < min)
                min = array[count];
    
            if(array[count] > max)
                max = array[count];
        }
    
        printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);
    
        return 0;
    }
    

    请注意,您在 for 循环 count &lt;= total 中的旧条件会产生未定义的行为,因为您在右侧再访问 1 个元素。

    我也忘了提及,但我想你在其他答案中读过它......当你将值设置为 min 和 max 为数组中的第一个数字时,你必须移动线在你之后读取数组。在您读取任何输入之前拥有min = array[0] 会将垃圾值设置为min,然后产生未定义的行为。


    编辑:如果您不确定所有输入序列是否以0 结尾,您应该更改for 条件以检查最后一个元素,在这种情况下,它不是0。你可以简单地通过替换 for 循环条件来做到这一点:

    // until here the code is the same.
    
    for (count = 0; count < total-(total != SIZE); count ++)
    
    // and from here the code is the same.
    

    有了这个,你的代码在以下情况下应该是这样的:

    • 如果SIZE 是10 并且您输入数字:1, 2, 3, 4, 0,for 循环将只检查1、2、3 和4。
    • 如果SIZE 是10 并且您输入数字:1, 2, 3, 4, 5, 6, 7, 8, 9, 10,for 循环将检查所有数字,因为表达式(total != SIZE) 的计算结果为0,因此total-(total != SIZE) 与@987654348 相同@。

    【讨论】:

      【解决方案4】:

      这个任务可以通过非常优化的代码来完成,你的代码有很多错误。但是这里我只是在你的代码中进行更正,并为每个更正提供 cmet,因此它可以帮助你找到它不工作的原因并帮助你理解程序。

      查看下面的代码,必须阅读代码中的所有cmets

      #include<stdio.h>
      #define SIZE 100
      int main()
      {
          int count = 0, store, array[SIZE], total;
          //int min = array[0], max = array[0];
          // above line will set garbase in min and max
           int min=0, max=0; // we will initialize it later
          do
          {
          printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
          scanf("%d", &store);
         if(store == 0){
             break;
           /* if you should not break here it put 0 in array so
             you always fiind 0 as min*/
          }
          array[count ++] = store;
          //above count ++ , its post increment so count is one more than total elements
          } while ( count < SIZE && store != 0 );
      
          total = count-1;
          /*count - 1 , reason , when you put last value in count,
          it incremented after that, because you are using post
          increment operator after that*/
          printf("Total number of values in array is %d.\n", total);
          if(total > 0)
          { /*it helps to initialize min with first element then
          it will update with your condition*/
            min = array[0];
            max = array[0];
          }
          for ( count = 0 ; count < total && array[count != 0] ; count ++)
          { // count < total, because loop starts from 0
          if(array[count] < min)
          {
              min = array[count];
          }
      
          if(array[count] > max)
          {
              max = array[count];
          }
          }
      
          printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);
          return 0;
      }
      

      如果有任何困惑,那么你应该在评论中提问,祝你好运。 在 Turbo-C 上检查代码

      下面是你的程序有一些变化的代码,它可以帮助你理解下一个层次,你可能会想出更好的方法。

      #include<stdio.h>
      #define SIZE 100
      int main()
      {
          int count = 0, store, array[SIZE], total;
          int min, max; // we will initialize it later
          do
          {
          printf("\nEnter integer number %d. Press 0 to exit: \n", (count + 1));
          scanf("%d", &store);
          if(store == 0){
           break;
          }
          array[count++] = store;
          if(count == 1){
            min = array[0];
            max = array[0];
          }
          if(store < min)
          {
              min = store;
          }
      
          if(store > max)
          {
              max = store;
          }
          } while ( count < SIZE);
          if(count > 0){
           printf("\nThe min value is %d.\nThe max value is %d.\n", min, max);
          }
          else{
           printf("No element in array");
          }
          return 0;
      }
      

      【讨论】:

      • 谢谢!这些都是很好的建议
      • 欢迎,如果它对你有用,请将答案标记为确认。
      猜你喜欢
      • 2016-07-24
      • 2016-03-03
      • 2017-08-18
      • 2018-05-03
      • 2016-05-25
      • 1970-01-01
      • 2020-03-07
      • 2021-06-18
      相关资源
      最近更新 更多