繁体   English   中英

在 C 中查找存储在用户输入数组中的最小值和最大值

[英]Finding minimum and maximum value stored in a user input array in C

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

  • 让用户输入数字直到他们输入 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;
}

在数组填充后移动 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

这个任务可以通过一个非常优化的代码来完成,你的代码有很多错误。 但在这里我只是对您的代码进行更正,并在每次更正时提供注释,这样可以帮助您找到它不起作用的原因并帮助您理解程序。

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

#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;
}

您的代码的问题是(我认为您打错了)for 循环中的第二个条件。 如果您测试array [count != 0] ,则表达式count != 0值可以是10因此您只需访问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 <= total中的旧条件会产生未定义的行为,因为您在右侧访问了 1 个元素。

我也忘了提及,但我猜你在其他答案中读过它......当你将值设置为minmax到数组中的第一个数字后,你必须移动该行。 在读取任何输入之前设置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 循环将只检查1234
  • 如果SIZE为 10 并且您输入数字: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,for循环将检查所有数字,因为表达式(total != SIZE)将评估为0因此total-(total != SIZE)total-0相同。

您的代码中有错误。 首先这个初始化未命中int min = array[0], max = array[0]; 如果没有array[0]值,则不能将其分配给其他变量。 这个初始化应该在do-while 。(否则就像使用未初始化的变量)

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

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

还要注意这里count<=total你正在传递你的数组的边界,这将导致未定义的行为。

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM