Question

EASY PERL QUESTIONS 1. Write a subroutine, called above_average, which takes a list of numbers and...

EASY PERL QUESTIONS

1. Write a subroutine, called above_average, which takes a list of numbers and returns the ones which are above the average (mean). (Hint: make another subroutine that calculates the average by dividing the total by the number of items.) Try your subroutine in this test program.

my @fred = above_average(1..10);

print "\@fred is @fred\n";

print "(Should be 6 7 8 9 10)\n";

my @barney = above_average(100, 1..10);

print "\@barney is @barney\n";

print "(Should be just 100)\n";

2. Write a subroutine named greet that welcomes the person you name by telling each new person the names of all the people it has previously greeted:

greet( "Fred" );

greet( "Barney" );

greet( "Wilma" );

greet( "Betty" );

This sequence of statements should print:

Hi Fred! You are the first one here!

Hi Barney! I've seen: Fred

Hi Wilma! I've seen: Fred Barney

Hi Betty! I've seen: Fred Barney Wilma

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#!/usr/local/bin/perl

#use diagnostics;
#use warnings;
use Math::Trig;
use utf8;
use strict;
use v5.10;

#exercise 1
#=for comment

sub total {
   my $sum = 0;
   foreach (@_) {
       $sum += $_;
   }
   $sum;
}

my @fred = qw{ 1 3 5 7 9 };
my $fred_total = total(@fred);
print "The total of \@fred is $fred_total.\n";

print "Enter some numbers on separate lines: ";
my $user_total = total(<STDIN>);
print "The total of those numbers is $user_total.\n";
print "\n\n";

#exercise 2
my $sum_of_1_to_1000 = total(1..1000);
print "\$sum_of_1_to_1000 = $sum_of_1_to_1000\n";
print "\n\n";

#=cut

#exercise 3
sub average {
   my $mean = 0;
   foreach (@_) {
       $mean += $_;
   }
   $mean /= @_;
}

sub above_average {
   my $mean = &average(@_);
   my @result;
   foreach (@_) {
       if ($_ > $mean) {
           push @result, $_;
       }
   }
   @result;
}

my @fred = &above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";

my @barney = above_average(100, 1..10);
print "\@barney is @barney\n";
print "(Should be just 100)\n";
print "\n\n";

#exercise 4
sub greet {
   state $last_name = undef;
   if ($last_name eq undef) {
       print "Hi $_[0]! You are the first one here!\n";
   } else {
       print "Hi $_[0]! $last_name is also here!\n";
   }
   $last_name = $_[0];
}

greet("Fred");
greet("Barney");
print "\n\n";

#exercise 5
sub greet2 {
   state $last_names = undef;
   if ($last_names eq undef) {
       print "Hi $_[0]! You are the first one here!\n";
   } else {
       print "Hi $_[0]! I've seen: $last_names\n";
   }
   if ($last_names ne undef) {
       $last_names .= " ";
   }
   $last_names .= $_[0];
}

greet2( "Fred" );
greet2( "Barney" );
greet2( "Wilma" );
greet2( "Betty" );
print "\n\n";

Add a comment
Know the answer?
Add Answer to:
EASY PERL QUESTIONS 1. Write a subroutine, called above_average, which takes a list of numbers and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • part one : Review Exercises 1. Write method called raggedCount that takes an integer n as...

    part one : Review Exercises 1. Write method called raggedCount that takes an integer n as argument and returns a ragged array of n rows of lengths 1, 2, 3, 4, ... , n and the whole array has the integers 1, 2, 3, 4, ... , n(n+1) 2 in row order. For example, raggedCount(4) should return the array: 1 2 3 4 5 6 7 8 9 10 1 2. Write a method called arrayConcat that takes as argument...

  • I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions...

    I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....

  • 1-Suppose you write an application in which one class contains code that keeps track of the...

    1-Suppose you write an application in which one class contains code that keeps track of the state of a board game, a separate class sets up a GUI to display the board, and a CSS is used to control the stylistic details of the GUI (for example, the color of the board.) This is an example of a.Duck Typing b.Separation of Concerns c.Functional Programming d.Polymorphism e.Enumerated Values 2-JUnit assertions should be designed so that they a.Fail (ie, are false) if...

  • Comprehensive Problem 6-52 (LO 6-1, LO 6-2, LO 6-3) [The following information applies to the questions...

    Comprehensive Problem 6-52 (LO 6-1, LO 6-2, LO 6-3) [The following information applies to the questions displayed below.] Read the following letter and help Shady Slim with his tax situation. Please assume that his gross income is $172,900 (which consists only of salary) for purposes of this problem. December 31, 2019 To the friendly student tax preparer: Hi, it’s Shady Slim again. I just got back from my 55th birthday party, and I’m told that you need some more information...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT