[Oc-pm] illegal division by zero?

Chris Grau chris at chrisgrau.com
Wed Sep 17 08:42:14 PDT 2008


On Tue, Sep 16, 2008 at 10:20:49PM -0700, Raul Ruiz Jr. wrote:
> I have a question? My instructor did not pass me on my script/library
> project because of "illegal division by zero on line 14 of
> obj13-lib.pl (my library) part of my script. 
> What am I doing wrong and can someone help me? 
[snip]
> Write a script called obj13-1.pl and a library called obj13-lib.pl.
> The library should contain a function that takes in an array of
> numbers (of arbitrary size).  The function will then calculate the
> average of the numbers, the total of all of the numbers added
> together, and a new array of numbers which is comprised of the other
> numbers divided by 2.

I had to reformat the function, because the indentation was painful to
me.

> sub sumIt {
>     @functionArray = @_;
>     foreach $line(@functionArray){
>         $functionSum += $line;
>         $count++
>     }
>     return ($functionSum,$functionSum / $count);
> }

The lack of use strict/variable declarations disturbs me, first of all.
What happens when sumIt() is called a second time?  More to the point at
hand, what happens when sumIt() is called with an empty list as an
argument?

Also, you're missing something.  The problem description lists three
things that are to be returned.  This sumIt() function only returns two.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.pm.org/pipermail/oc-pm/attachments/20080917/74b79a90/attachment.bin>


More information about the Oc-pm mailing list