Error in finding minimum element in perl -
i trying minimum , maximum element in array of elements in perl, every thing working fine minimum element not getting printed:
@array1=(); $i=0; $array1[$i]=<stdin>; $sum=$minimum=$maximum=$array1[$i]; while($array1[$i]!=" ") { $i=$i+1; $array1[$i]=<stdin>; $sum=$sum+$array1[$i]; if($array1[$i]>$maximum) { $maximum=$array1[$i]; } if($array1[$i]<$minimum) { $minimum=$array1[$i]; } if($array1[$i]==" ") { last; } } print("entered numbers : ",@array1); print("sum of numbers : ",$sum); print "\n"; print("minimum number : ",$minimum); print "\n"; print("maximum number : ",$maximum);
consider occurs when enter space in loop. assign $array1[$i]. check min , max. if have string , numeric comparisons, convert string number if can (such "12" 12). if have string not number (such space) convert 0.
i suspect typing positive number, when enter space, if condition minimum true , space placed in minimum value. if put in negative number, print out negative number code.
if move check see if finished after read in item , before checks, work better , not go through comparisons don't need comparisons/assignments since finished , entry not number.
Comments
Post a Comment