ads/Assignment4%20(1.pdf Watch Anime Onlins WebAssign FMones- Watch M Savannah S
ID: 3718849 • Letter: A
Question
ads/Assignment4%20(1.pdf Watch Anime Onlins WebAssign FMones- Watch M Savannah State Unic Course Home 2 Remin Perl Programming CSCI 2215 Section Name: Assignment 4 (100 pts) Instructions: Submit an electronic copy of the assignment to the Dropbox Date: CHECK CALENDAR FOR DUE DATE 1. Hotels are often rated using stars to represent their score. A five-star hotel may have a king-size bed, a kitchen, and two TVs, a one-star hotel may have cockroaches and a leaky roof. a. Write a subroutine called printstar that will produce a histogram to show the star rating for hotels shown in the following hash. The printstar function will be given two parameters: the name of the hotel and the number of its star rating (Hint: sort the hash keys into an array. Use a loop to iterate through the keys, calling the printstar function for cach iteration.) Pillowmint Lodge"->"5" "Buxton Suites" "5". "The Middletonam" "3" "Notchbelow"4 "Rancho El Cheapo"-"I" "Pile Inn" %hotels OUTPUT) e gExplanation / Answer
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
sub printstar
{
printf "%-18s| ",$_[0];
for(my $i=0;$i<$_[1];$i=$i+1)
{
print "*";
}
printf " ";
}
#create hash for hotels
my %hotels=("Pillowmint lodge" => 5 ,"Buxten suites" =>5 , "The middletonian" =>3,"Notchbelow" =>4,"Rancho El Cheapo" =>1 , "Pile Inn" =>2 );
my $count=keys %hotels; # Count Total hotels on hash
my @hotel= keys %hotels; # store key of hash (hotel name is key here) into an array named as hotel
my @star= values %hotels ; # store value of hash (star of hotel is value her) into an array named as star
print "Hotel Category ---------------------------- ";
for( my $i=0; $i < $count ;$i=$i+1)
{
printstar($hotel[$i],$star[$i]); # calling a printstar function for key and value pair
}
print "--------------------------- ";
print " Hotel Category ----------------------------- ";
# sorting of hotels according to star in revrese order
foreach my $hotel ( reverse sort { $hotels{$a} <=> $hotels{$b} } keys %hotels )
{
printstar($hotel,$hotels{$hotel});
}
print "----------------------------- ";
# =for comments =cut use for multiline comments as shown below
=for comment
OUTPUT
Hotel Category
----------------------------
Notchbelow | ****
Pile Inn | **
Rancho El Cheapo | *
Buxten suites | *****
Pillowmint lodge | *****
The middletonian | ***
---------------------------
Hotel Category
-----------------------------
Pillowmint lodge | *****
Buxten suites | *****
Notchbelow | ****
The middletonian | ***
Pile Inn | **
Rancho El Cheapo | *
-----------------------------
=cut
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.