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

Re: Printing largest number in array using loops and if statement

by hossman (Prior)
on Jan 16, 2010 at 18:17 UTC ( #817787=note: print w/ replies, xml ) Need Help??


in reply to Printing largest number in array using loops and if statement

what if the input array contains negative numbers?

start with $big = $array[0] instead.


Comment on Re: Printing largest number in array using loops and if statement
Download Code
Re^2: Printing largest number in array using loops and if statement
by CountZero (Bishop) on Jan 16, 2010 at 19:24 UTC
    start with $big = $array[0] instead
    You spotted the logical error!. Well done.

    But now your loop has one iteration too many as the first time into the loop the test necessarily fails.

    If you do not have to preserve the original array, you could do $big = shift @array; instead.

    We will leave it to the OP to find a way to avoid the unnecessariy extra turn of the loop and still maintain the original array (without using an extra variable to save the shifted value).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Of course, but as this is an exercise, it is good to think the whole algorithm through in all its consequences and see what effects even small changes in the code have on the logic of the algorithm. Otherwise you are only cargo-culting.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^2: Printing largest number in array using loops and if statement
by matze77 (Friar) on Jan 17, 2010 at 08:21 UTC
    Yes. I was aware of that. My thought was compare it against an undefined variable? Would this be a possible solution. Maybe i could also compare with: "Greatest negative number perl could use"?. I like your solution its good to see what other people think about the same "problem" even if it is a really small one in this case ...

    Thanks
    MH

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://817787]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-21 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (100 votes), past polls