Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found