Thursday, October 20, 2022

Find maximum value from an array using perl

 

 #Returns maximum value from Array passed as parameter
  sub Find_Maximum {

  my @list = @_;

  $max = $list[0];

  for ($i = 1; $i <= $#list; $i++) { #iterate over all the array elements

    if ($list[$i] > $max) {                 #check if current element is greater than the already
      #stored max value
      $max = $list[$i];                     # if yes then update the max value to current element
    }
  }
  return $max;                           #return the maximum value
}#end of Find_Maximum()

@array = (15, 6, 3, 21, 19, 4);
print Find_Maximum (@array);

No comments:

Post a Comment