### Re: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.

by Loops (Curate)
 on Nov 20, 2014 at 02:37 UTC ( #1107854=note: 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.

• Comment on Re: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.

Replies are listed 'Best First'.
Re^2: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.
by AnomalousMonk (Bishop) on Nov 20, 2014 at 13:51 UTC

As long as List::Util is being brought into play, why not use sum() as well as min() and max()? Just push entries to an array, say @numbers, as long as conditions for continued entry are met, then, after a test for no numbers at all having been entered,
print 'The average of the numbers was ', sum(@numbers) / @numbers;
print 'The largest number was ',  max @numbers;
print 'The smallest number was ', min @numbers;
(Admittedly, this approach may not be appropriate for homework.)

Why even specify the list twice? Make it an odd count(-.

```require List::MoreUtils ;
printf "min: %f ; max : %f\n"
, List::MoreUtils::minmax @num
;
Re^2: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.
by SuicideJunkie (Vicar) on Nov 20, 2014 at 19:57 UTC

Personally, I'd want to set the min and max to undef.

I then have a min/max function that skips over undefs in the input list (rather than treating undef as zero). If everything in the list is undef, then the result is undef. Otherwise the min/max of the defined values is returned.

Yeah, then you don't even really need to set them to undef, since as others have shown in their code there is already a decent way to test if it's the first iteration. Or you can just prime the values from the first input to begin with and start iterating from the second value. What I was describing was a more general idea that has the benefit of simplicity and avoiding an extra conditional check in every iteration.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1107854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-08-19 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?