For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > >>>>****Getting the Smallest number and the Greatest froma series of numb









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author >>>>****Getting the Smallest number and the Greatest froma series of numb
masterofall

2006-02-21, 6:57 pm

*****so far i got the code on the bottum but my questions is when i run
it and try to get the largest/greatest it gives me the word end as a
number which i want a number not the word end when i want it to
stop*********>>>>>>>>>>>>
[color=darkred]

#!usr/loacl/bin/perl

$total = 0;
$numcounter = 0;
$largest= $number;


print "Enter a number or enter end: ";
chomp ( $number = <STDIN> );



until ($number eq "end")
{
$numcounter++;
$total += $number;
print "Enter a number or enter end: ";
chomp ( $number = <STDIN> );
}

if ( $numcounter != 0)
{

$average = $total / $numcounter;

print " \nTotal numbers inputed $numcounter";
print " \n The averaged is $average.\n ";

}
if ($largest < $number) {

$largest = $number ;
}
else {
print "The smallest number is $number";

}




$in = <stdin>;

masterofall

2006-02-21, 6:57 pm


masterofall wrote:
> *****so far i got the code on the bottum but my questions is when i run
> it and try to get the largest/greatest number from the series it gives me the word end as a number which i want a number not the word end *********>>>>>>>>>>>>
>
>
> #!usr/loacl/bin/perl
>
> $total = 0;
> $numcounter = 0;
> $largest= $number;
>
>
> print "Enter a number or enter end: ";
> chomp ( $number = <STDIN> );
>
>
>
> until ($number eq "end")
> {
> $numcounter++;
> $total += $number;
> print "Enter a number or enter end: ";
> chomp ( $number = <STDIN> );
> }
>
> if ( $numcounter != 0)
> {
>
> $average = $total / $numcounter;
>
> print " \nTotal numbers inputed $numcounter";
> print " \n The averaged is $average.\n ";
>
> }
> if ($largest < $number) {
>
> $largest = $number ;
> }
> else {
> print "The smallest number is $number";
>
> }
>
>
>
>
> $in = <stdin>;


usenet@DavidFilmer.com

2006-02-21, 6:57 pm

masterofall wrote:[color=darkred]

Is this a learning or homework exercise? Because, if it's not, you
should be using List::Util, which provides a max() and min() function
(or use maxstr() and minstr() if the list is alphanumeric).

See
http://search.cpan.org/~gbarr/Scala...ib/List/Util.pm

--
http://DavidFilmer.com

usenet@DavidFilmer.com

2006-02-21, 6:57 pm

masterofall wrote:[color=darkred]

By the way, you really need to think the logic through here. You've got
much more serious problems than the one you think you have.

masterofall

2006-02-22, 6:56 pm

i never heard of list ::Util or don't know how it works

is there a more simpler way to do it.with <, or > or a if statement?

i don't see no other problems here.
what more serious problems do u see that i don't?

usenet@DavidFilmer.com

2006-02-22, 6:56 pm

masterofall wrote:
> i never heard of list ::Util or don't know how it works


You've never heard of CPAN? You've never used a module? Well, Perl will
tell you about these things:

perldoc -q 'install a module from CPAN'

I gave you the link to the List::Util docs on CPAN. But you can't
really use it because you have forgotten to, um, build a list.
Therefore, it is impossible for your code (as it is written) to
determine the largest/smallest value (all you can determine is the
average, and not very well, I might add).

If you, um, build a list, you can use List::Util to find all your
information:

my $min = min(@list);
my $max = max(@list);
my $avg = sum(@list) / @list; #make sure @list is not empty!

--
http://DavidFilmer.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com