C语言 C编程数组的最小值和最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19462948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:45:22  来源:igfitidea点击:

C Programming min and max of array

carraysmaxmin

提问by Sal Z.

I am trying to write a program that fills an array with 11 spots with random numbers between 1-100. It seems like the random stuff works, and the minimum works, but the maximum I am getting crazy high numbers that aren't even part of the 11 numbers that were tossed in the array.

我正在尝试编写一个程序,用 11 个点和 1-100 之间的随机数填充一个数组。似乎随机的东西有效,最小值有效,但最大值我得到了疯狂的高数字,这些数字甚至不是被扔到数组中的 11 个数字的一​​部分。

Not quite sure what the problem is, but I am pretty sure it is something ridiculously simple I am looking past.

不太确定问题是什么,但我很确定这是我正在寻找的简单可笑的事情。

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

int main( void )
{
   int i  = 0;
   int a[11];

   int min = a[0];
   int max = a[0];

   srandom( (unsigned) time(NULL) );

   for (i=0;i<11;i++)
   {
       a[i]=random( ) % 100 ;

       printf("%d\n", a[i]);

       if (a[i] > max)
       {
           max = a[i];
       }
       else if (a[i] < min)
       {
           min = a[i];
       }
   }

   printf("Min: %d\n", min);
   printf("Max: %d\n", max);

   return ( 0 ) ;
} 

OUTPUT:

输出:

16
28
27
58
8
53
76
35
27
19
41
Min: 8
Max: 152908968

回答by Jerry Coffin

Your problem is right here:

你的问题就在这里:

int a[11];

int min = a[0];
int max = a[0];

You're using a[0]to initialize minand max, before a[0]itself has been initialized.

您正在使用a[0]来初始化minand max,在a[0]其本身被初始化之前。

Initialize a[0], then set minand maxto its value, then proceed from there.

初始化a[0],然后设置minmax为其值,然后从那里继续。

回答by sukhvir

max is initialized to a very high value .. initialize max to 0;

max 被初始化为一个非常高的值 .. 将 max 初始化为 0;

int max = 0;

Here is the fixed code :

这是固定代码:

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

int main( void )
{
   int i  = 0;
   int a[11];

   int min = 0;
   int max = 0;

   srandom( (unsigned) time(NULL) );
   a[0]=random( ) % 100 ;
   min = a[0];

for (i=1;i<11;i++)
     {
       a[i]=random( ) % 100 ;


       printf("%d\n", a[i]);

       if (a[i] > max)
            {
          max = a[i];
            }
       if (a[i] < min)
            {
          min = a[i];
            }
    }
            printf("Min: %d\n", min);
            printf("Max: %d\n", max);

return 0;
}

Output:

输出:

Notra:Desktop Sukhvir$ ./try
82
91
33
8
60
48
60
6
59
62
60
Min: 6
Max: 91

回答by Umer Farooq

First you are initializing min& maxwith garbage values& then you are comparing them with other elements of array?

首先,您使用垃圾值初始化min&然后将它们与数组的其他元素进行比较?max

  • First assign values to each elementof the array.

  • Set min & max values to a[0](for simplicity sake).

  • Then start comparisonof max with other elements of the array.
  • 首先为数组的每个元素赋值

  • 最小值和最大值设置为 a[0](为简单起见)。

  • 然后开始将 max 与数组的其他元素进行比较

Try the following code:

试试下面的代码:

int main( void )
{
   int i  = 0;
   int a[11];

   int min;
   int max;

   srandom( (unsigned) time(NULL) );

for (i=0;i<11;i++)
       a[i]=random( ) % 100 ;

min = a[0];
max = a[0];

for (i=1;i<11;i++)
     {
       printf("%d\n", a[i]);

       if (a[i] > max)
            {
          max = a[i];
            }
       else if (a[i] < min)
            {
          min = a[i];
            }
    }
            printf("Min: %d\n", min);
            printf("Max: %d\n", max);

return ( 0 ) ;

回答by Maxime Chéramy

Just for the alternative since you already got 4 correct answers.

只是为了替代,因为您已经得到了 4 个正确答案。

In order to initialize your variables minand max, you can either:

为了初始化您的变量minand max,您可以:

  • initialize them to get the value of a[0] (after a[0] has received its value ;)).
  • initialize min to 0 and max to 100 since you know that your values can't be less than 0 or greater than 100.
  • add an extra condition in your test: if (i == 0 || a[i] > max)and start your loop from 0.
  • 初始化它们以获取 a[0] 的值(在 a[0] 收到它的值之后;))。
  • 将 min 初始化为 0,将 max 初始化为 100,因为您知道您的值不能小于 0 或大于 100。
  • 在您的测试中添加一个额外的条件:if (i == 0 || a[i] > max)并从 0 开始您的循环。

The two other alternatives I am providing allows you to deal with cases where the data are not available yet. For example, if the values come from the user or the network or whatever.

我提供的另外两个替代方案允许您处理数据尚不可用的情况。例如,如果值来自用户或网络或其他任何东西。

回答by haccks

Problem is your code snippet

问题是你的代码片段

int min = a[0];
int max = a[0];  

you are assigning a[0]to maxand minbefore initializing elements of a. First initialize elements of array then you can assign its elements to other variables.

您要分配a[0]maxmin初始化的元素之前a。首先初始化数组的元素,然后您可以将其元素分配给其他变量。

 int min ;
 int max ;

 srandom( (unsigned) time(NULL) );   

 min = max = a[0] = random() % 100 + 1;  

 for (i = 1; i < 11; i++) 
 { 
        a[i]=random( ) % 100  + 1; // to generate numbers between 1 to 100  
        ...

回答by Alex

//Just another version...

void minArray(int v[])
{

    int m=100,len=0;   //for max, put m=0
    for(int i=0;v[i];i++)
        len++;

    for(int i=0;i<len;i++)
        if(v[i]<m)   //for max, change '<' to '>'
            m=v[i];

    printf("%d",m);
}

int main(void)
{
    int v[100]={5,2,3,4,22,5};
    minArray(v);

    return 0;
}

回答by Alex

Your problem lies with the min and max. You are assigning them values which you don't know. You are filling the array with values more than 0 and less than 100. So, Why not set the values of min as 100 and max as 0. So that whenever the program finds a number greater or lesser than the min or max. The program will change their values.

你的问题在于最小值和最大值。您正在为它们分配您不知道的值。您正在用大于 0 且小于 100 的值填充数组。那么,为什么不将 min 的值设置为 100,将 max 的值设置为 0。这样,无论何时程序发现一个数字大于或小于最小值或最大值。该程序将更改它们的值。

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

int main( void )
{
   int i  = 0;
   int a[11];

   int min = 100;
   int max = 0;

   srandom( (unsigned) time(NULL) );

   for (i=0;i<11;i++)
   {
       a[i]=random( ) % 100 ;

       printf("%d\n", a[i]);

       if (a[i] > max)
       {
           max = a[i];
       }
       else if (a[i] < min)
       {
           min = a[i];
       }
   }

   printf("Min: %d\n", min);
   printf("Max: %d\n", max);

   return ( 0 ) ;
} 

Output:

输出:

gcc version 4.6.3

36
47
42
64
21
78
66
23
44
20
95
Min: 20
Max: 95