Perl calculate and average problem

Discussion in 'Perl Misc' started by ed, Sep 30, 2005.

  1. ed

    ed Guest

    I'm trying to understand perl and I'm pretty much confused. I'm trying
    to write a script that takes an array of numbers (arbitrary in size).
    The function will calculate the average of the numbers, the total of
    all the numbers added together, and a new array of numbers which is the
    other numbers divided by 2. It will then return a new list with all of
    the information, and a new array of number, the total. The script
    file, should get a list of numbers from the user (either via STDIN or a
    list of arguments.)
     
    ed, Sep 30, 2005
    #1
    1. Advertising

  2. ed wrote:
    > I'm trying to understand perl and I'm pretty much confused. I'm trying
    > to write a script that takes an array of numbers (arbitrary in size).
    > The function will calculate the average of the numbers, the total of
    > all the numbers added together, and a new array of numbers which is the
    > other numbers divided by 2. It will then return a new list with all of
    > the information, and a new array of number, the total. The script
    > file, should get a list of numbers from the user (either via STDIN or a
    > list of arguments.)


    Thanks for letting us know.

    Where is the code you've written?

    Have you read the posting guidelines for this group?
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 30, 2005
    #2
    1. Advertising

  3. ed

    ed Guest

    Well here is what I have so far:

    @params = (1,2,3,4,5,7,8,9);

    foreach $digits(@params){
    $total = numbers($digits);
    print "$numbers\n";
    }

    sub numbers (){
    my $digits = shift(@_);

    foreach $params(@params){
    $digits =+ @params;
    }

    return $total
    }

    I can't get the average of the array to even get to the other part of
    the code to divide the total.
     
    ed, Sep 30, 2005
    #3
  4. ed wrote:
    > Well here is what I have so far:


    # Tell us something more ...
    use warnings;
    use strict;

    > @params = (1,2,3,4,5,7,8,9);
    >
    > foreach $digits(@params){
    > $total = numbers($digits);
    > print "$numbers\n";


    Where does $numbers come from?
    The above pragmas would have told you it isn't defined.

    > }
    >
    > sub numbers (){
    > my $digits = shift(@_);
    >
    > foreach $params(@params){
    > $digits =+ @params;


    You _do_ know that @params in scalar context is the number of elements
    in @params?

    > }
    >
    > return $total
    > }
    >
    > I can't get the average of the array to even get to the other part of
    > the code to divide the total.


    Let's see if we can dig up something based upon your initial request:
    "calculate the average of the numbers, the total of
    all the numbers added together, and a new array of numbers which is the
    other numbers divided by 2."

    my $sum = 0, $avg = 0;
    my @half;
    foreach (@params) {
    $sum += $_;
    push @half, $_ / 2;
    }
    $avg = $sum / @params;
    #
    # $avg: the average of the numbers
    # $sum: the total of all the numbers added together
    # @half: a new array of numbers which is the other numbers divided by 2
    print "average: ", $avg, "\n";
    print "total: ", $sum, "\n";
    print "new array: ", join(" ", @half), "\n";


    I can't, for the heck of it, find out what you mean by
    "It will then return a new list with all of
    the information, and a new array of number, the total."

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Sep 30, 2005
    #4
  5. [ Please provide context when replying to a message, as the posting
    guidelines recommend! Those who may be able to help don't usually access
    this group via Google. ]

    ed wrote:
    > Gunnar Hjalmarsson wrote:
    >> ed wrote:
    >>> I'm trying
    >>> to write a script that takes an array of numbers (arbitrary in size).
    >>> The function will calculate the average of the numbers, the total of
    >>> all the numbers added together, and a new array of numbers which is the
    >>> other numbers divided by 2.

    >>
    >> Where is the code you've written?

    >
    > Well here is what I have so far:


    <code without strictures and warnings snipped>

    >> Have you read the posting guidelines for this group?
    >> http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


    Why didn't you make an attempt to follow the posting guidelines, when
    your attention had been called to them?

    Why did you choose to even ignore that question?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 30, 2005
    #5
  6. ed

    ed Guest

    The course I'm taking hasn't gotten to the use of warnings/strict but I
    have read a little about it.
    As far as the number go, my frustration level is way up there and I've
    changed my script so many times that at this point not sure where
    numbers came from.
    The way I understand this "You _do_ know that @params in scalar context
    is the number of elements in @params? " is that the the number is eight
    (total elements), not the numbers 1,2,3,4,5,7,8,9? I was told to get a
    list of numbers from STDIN or a list or arguments, is my array not the
    correct way to go?
     
    ed, Sep 30, 2005
    #6
  7. "ed" <> wrote in
    news::

    > The course I'm taking hasn't gotten to the use of warnings/strict but
    > I have read a little about it.


    Ask for your money back. The *first* step in writing any Perl program
    must be to enable strictures and warnings.

    Have you read the posting guidelines, yet. They explain how you can help
    yourself, but more importanttly, help others help you.

    > As far as the number go, my frustration level is way up there and I've
    > changed my script so many times that at this point not sure where
    > numbers came from.


    Well, you need to take five minutes, read the guidelines, and follow
    them step by step. Then, go over your assignment, and come up with a
    spec for your program which others can understand. Then, follow the spec
    step by step to write it (with strictures and warnings enabled).

    That way, when you his a snag, others will be able to help you.

    > The way I understand this "You _do_ know that @params in scalar
    > context is the number of elements in @params? " is that the the
    > number is eight (total elements), not the numbers 1,2,3,4,5,7,8,9?


    If @params consists of (1, 2, 3, 4, 5, 6, 7, 8, 9), then @params has 9
    elements. $params[0] is the first element. $params[8] is the last
    element.

    You'll need to start quoting an appropriate amount of context, and
    responding to specific questions others have asked to have any hope of
    staying out of killfiles.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 30, 2005
    #7
  8. ed

    Guest

    Josef Moellers <> wrote:
    > Let's see if we can dig up something based upon your initial request:
    > "calculate the average of the numbers, the total of
    > all the numbers added together, and a new array of numbers which is the
    > other numbers divided by 2."


    > my $sum = 0, $avg = 0;


    This should be either:

    my $sum = 0, my $avg =0;
    or

    my ($sum, $avg);

    otherwise $avg will fail 'use strict'.

    Axel
     
    , Sep 30, 2005
    #8
  9. ed

    ed Guest

    Thanks, I appreciate your help.
     
    ed, Sep 30, 2005
    #9
  10. ed wrote:

    Please (more visibly) quote some context when replying.

    > The course I'm taking hasn't gotten to the use of warnings/strict but I
    > have read a little about it.
    > As far as the number go, my frustration level is way up there and I've
    > changed my script so many times that at this point not sure where
    > numbers came from.
    > The way I understand this "You _do_ know that @params in scalar context
    > is the number of elements in @params? " is that the the number is eight
    > (total elements), not the numbers 1,2,3,4,5,7,8,9? I was told to get a
    > list of numbers from STDIN or a list or arguments, is my array not the
    > correct way to go?
    >


    Well, the choice of data structure surely is an important part of the
    analysis and design phase of a program, but in Perl, an array would be a
    good thing to use.

    What I was aiming at was your use of @params here:
    > $digits =+ @params;


    @params has a different meaning depending on whether it is used in list
    context (where it denotes the entire array) or in scalar context (where
    it denotes the number of elements in @params).
    Finding out the current context is not always easy, but trying to add
    something to a scalar definitely is scalar context and from your code,
    adding the size of the array for each element of the array is definitely
    not what you wanted to do.
    It may also have been a mis-spelling, as
    > foreach $params(@params){

    assigns each arramy element of "@params" to a scalar variable "$params",
    so your code would have been better if you'd written
    > $digits += $params;

    (I'd have use "$param" instead of "$params", as the variable contains a
    single parameter only in each loop iteration).

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Oct 4, 2005
    #10
  11. Josef Moellers <> wrote:

    > Finding out the current context is not always easy,



    Sure it is. You just replace the construct that you are wondering
    about with:

    context()

    where

    sub context {
    warn wantarray() ? 'list context' : 'scalar context'
    }



    :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 4, 2005
    #11
  12. Tad McClellan wrote:
    > Josef Moellers <> wrote:
    >
    >>Finding out the current context is not always easy,

    >
    >
    > Sure it is. You just replace the construct that you are wondering
    > about with:
    >
    > context()
    >
    > where
    >
    > sub context {
    > warn wantarray() ? 'list context' : 'scalar context'
    > }


    You missed one Tad. ;-)

    sub context {
    warn defined wantarray() ? wantarray() ? 'list' : 'scalar' : 'void', '
    context'
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 4, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    311
    \/Gogineni\/
    Nov 22, 2005
  2. gaga
    Replies:
    4
    Views:
    412
    James Kanze
    Apr 6, 2007
  3. Bill Cunningham

    average problem

    Bill Cunningham, Jul 24, 2009, in forum: C Programming
    Replies:
    13
    Views:
    565
    Keith Thompson
    Jul 24, 2009
  4. Erol Akman
    Replies:
    9
    Views:
    230
    Erol Akman
    Mar 25, 2009
  5. ela
    Replies:
    8
    Views:
    207
Loading...

Share This Page