So as you consider each number now, you're adding it to $total so that it is part of the final average. Similarly you need to keep the largest and smallest numbers you've seen so far. With each new number, after adding it to the $total, see if it qualifies as either the new largest or smallest, and update that value if so.

You can construct your own "if" statements to see if the new number is greater than the current $largest or not. Or you could use List::Util which has a min (and a max) function which return the smallest (and largest) value of the list you pass in. For instance:

use List::Util qw(min max); print max(4,8,2,33,1); # prints 33

So each time you consider a new number you can update the $smallest number so far to either be the current value of $smallest, or the new number:

$smallest = min ($smallest, $number);

Unfortunately because $smallest is set to 0 at the start of your program, no value above zero will ever show up as the smallest input! You could start it off with a value of 10,000 and just hope nobody enters a number bigger than that. But a better way is to start $smallest off with the largest number possible, so that the first input it sees will naturally be the smallest-so-far. In Perl there is a funny notation for this largest number possible: '+inf', and for the smallest possible, minus infinity or '-inf'.

Therefore you'd need to remove $smallest and $largest from where they are initialized to zero now, and add a line like:

my ( $largest, $smallest ) = ('-inf', '+inf');

So that the logic you use to find the largest and smallest is sure to find values that fit.


In reply to Re: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated. by Loops
in thread I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated. by pcoady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.