#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    1
    Rep Power
    0

    Simple Integer Average


    Here is the script I have so far.... the question is on the bottom. Any idea how to finish it?

    #!/usr/bin/perl -w
    $count = 0;
    $avg = 0;
    while ((print "enter line:"),($line = <STDIN>) !~ /^\s*quit\s*$/i)
    {
    if($line =~ /^\s*$/)
    {
    next;
    }
    if($line !~ /^\s*([+-]?\d+)\s*?([+-]?\d*)\s*$/)
    {
    print "ERROR! Pls enter valid data:\n";
    next;
    }
    print "$1 $2";
    if(defined($2))
    {
    if($2 !~ /^[+-]?\d+$/)
    {
    $tot = $1 ;


    }
    else
    {
    $tot = $2;
    $count++;

    }

    }
    $count++;




    }
    if($count != 0)
    {
    $ave = $tot/$count;
    print "The total numbers entered = $count";
    print "the average = $ave";
    }


    **************************************************************************8
    QUESTION BELOW


    write a pearl script with a "quit" loop . On each line of input,the user may
    enter 1 or 2 integer tokens which may be signed. Compute the average of
    integers from the correctly entered lines. Use the same input rules as
    problem #1. Present the average on the screen .. if there is one to be
    computed!!

    Dont worry about the number of digits displayed in the averaqge.
    Hint:can use perl function defined().

    Rules.
    a) it will allow lines to be entered until the user enters "quit"
    b)
    If user enters an empty line ,simply repromt with no error message.
    c)If the user enters an invalid line reprompt after giving error msg.

    d)Valid lines will have the one or two tokens described and may have any
    amount of whitespace before/between/after the two tokens.

    e) After the user quits out of the loop, present all of the valid first
    tokens from valid lines (one per line), then all of the valid second tokens
    from the valid lines (one per line), and the number of valid lines.
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,894
    Rep Power
    6444
    OK, this is homework, and you couldn't be bothered to tell us where you think the problem is, or how you'd like to go about solving it ... c'mon, you would have had a better chance getting help if you made an effort ...
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  4. #3
  5. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Happy birthday. Should be easy to explain to your professor.
    Code:
    perl -e '@a;@b;while(chomp($i=<STDIN>)&&$i!~m/quit/i){next unless $i;if($i=~m/[^\d-\s]/){print"error\n";next}@i=split(/\s+/,$i);my$f;map{$f+=$_}@i;if(@i==2){print$f/@i."\n";push@a,$i[0];push@b,$i[1];}}print map{"$_\n"}"first",@a,"second",@b,"total",scalar @a'
    And yes - it works to the requirements of the assignment.

    Comments on this post

    • Axweildr agrees : ;)

IMN logo majestic logo threadwatch logo seochat tools logo