Assignment Five: First Perl Program Your task is to write a Perl program that wi
ID: 3696844 • Letter: A
Question
Assignment Five: First Perl Program Your task is to write a Perl program that will allow a user to enter pairs of three-dimensional points and display the distances between them. Specifically, the program will display a welcome message, and then repeatedly display a prompt to its user, asking the user to either enter the letter "q" to quit the program, or to enter "c" to continue. If the user enters a "c", the program then prompts the user for the x-coordinate of the first point, then the y-coordinate, then the z-coordinate, and then prompts three more times for the x, y, and z coordinates of the second point. Once it has these values, it computes the distance between the two points and outputs that distance on the screen with a short message such as "the distance is ...". If the user enters a "q" instead of a "c", the program should terminate. A number is valid if it is a valid numeric literal (see the notes for what this means.) Perl has a function named squareroot ()that can be used for this purpose. Error checking: If the user enters any other character when it is waiting for the 'q' or the 'c', it should ignore it and display the prompt again. It should keep doing this until the user enters a valid character ('q' or 'c'.) If, when the program is expecting the user to enter a coordinate, which must be a number, the user enters something other than a number, the program will use the value into which Perl converts that non-number as the user's input. To write this program, you will have to study the pieces of the math q program that we studied in class. By studying that program, you will be able to write this one, because this one is a simplification of the math q program. Remember that the math q program is in the demos/perldemos directory, in subdirectory chapter03. It is called mathq_v2.pl. Extra Credit For 20% extra credit, in the event that the user does not enter a valid number, the program will repeatedly ask the user to enter a valid number until he or she does.Explanation / Answer
use strict;
use warnings;
# asking for characters
while(defined(my $answer = prompt("type c to continue or quit to exit: "))) {
if($answer eq "c"){
my $result = distance();
print "distance is: $result ";
} elsif($answer eq "q"){
print "Bye Bye ";
last;
}
}
sub prompt {
my ($challenge) = @_;
local $| = 1; # set autoflush;
print $challenge;
chomp( my $answer = <STDIN> // return undef);
return $answer;
}
#calculating the distance between two coordinates
sub distance
{
print "Enter First coordinate ";
print "Enter x: ";
my $x1 = <STDIN> ;
print "Enter y: ";
my $y1 = <STDIN> ;
print "Enter z: ";
my $z1 = <STDIN> ;
print "Enter Second coordinate ";
print "Enter x: ";
my $x2 = <STDIN> ;
print "Enter y: ";
my $y2 = <STDIN> ;
print "Enter z: ";
my $z2 = <STDIN> ;
my $res = sqrt(($x2-$x1)*($x2-$x1) + ($y2-$y1)*($y2-$y1) + ($z2-$z1)*($z2-$z1));
return $res ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.