For this I need a program to read in an unknown number of non-negative values from the user until a negative number is entered. Once a negative number is entered the program needs to print out three values: the largest, smallest, and average of the numbers.

This is what I have so far. I am unsure on how to get the program to recognize the largest and smallest.

#!/usr/bin/perl use Modern::Perl; my ( $total, $howmany, $largest, $smallest, $number, $average ) = ( 0, + 0, 0, 0, 0, 0 ); print "Please enter a number or -1 to end: "; chomp ( $number = <> ); while ( $number != -1 ) { $total += $number; $howmany ++; print "Please enter a number or -1 to end: "; chomp ( $number = <> ); } if ( $howmany == 0 ) { say "There were no numbers to average."; } else { say "The average of the numbers was ", $total / $howmany; say "The largest number was ", $largest; say "The smallest number was ", $smallest;

something like this is what I am thinking. for (my $x = 9999999; $x >= 1 ; $x-- ) { say $x } Im just not sure how to continue it through all the variables as they get larger


In reply to 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.