dongxian6741 2016-02-10 03:21
浏览 755
已采纳

通过for循环查找数组中的最大值和最小值

I want to find the maximum and minimum value by the for loop, but problem is that when array value is start from a maximum number like (100,30,50,60) then output is the correct first maximum value then, minimum value.

Otherwise output first minimum value, then maximum value.

any idea?

    //Number serial.
    $number = array(10,15,20,100,25,30);

    for ($i=0; $i<count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] <br>" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] <br>";
        }
    }
//Output: 
//The min number is 10
//The max number is 100 

When array value is start from a maximum number.

 //Number serial.
    $number = array(100,10,15,20,25,30);

    for ($i=0; $i<count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] <br>" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] <br>";
        }
    }
//Output:
//The max number is 100
//The min number is 10
  • 写回答

2条回答 默认 最新

  • duangelin7513 2016-02-10 03:30
    关注

    Actually I think you dont need the for loop to begin with.... You can do something simple like this:

    $number = array(15,20,100,10,25,30);
    $max = max($number);
    $min = min($number);
    echo "min value is $min <br/>";
    echo "max value is $max <br/>";
    

    Update:

    But if you want to flexibly, echo the statements according to the index of the min and max values then you can do something like this:

    $number = array(15,20,100,10,25,30);
    $max = max($number);
    $min = min($number);
    $max_idx = array_search($max,$number);
    $min_idx = array_search($min,$number);
    if($max_idx < $min_idx){
      echo "max value is $max 
    ";
      echo "min value is $min 
    ";
    }else{
      echo "min value is $min 
    ";
      echo "max value is $max 
    ";
    }
    

    What this will do is, it will get the indices of the min and max values and depending on which is encountered first, it will print that statement first.

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配