Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to write a Perl program that will allow a user to enter pairs of th

ID: 3690301 • Letter: Y

Question

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.) The distance between the points (x_1,y_1,z_1) and (x_2,y_2,z_2) is given by the formula squareroot(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2 Perl has a function named sqrt() 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 mathq 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 mathq program. Remember that the mathq 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. Submitting the Solution Name your program hwk5_us ername .pl and put it in the directory/data/biocs/b/student.accounts/cs132/projects/hwk5. Give your file permission rw-------(600). It does not have to be executable; I can take care of that. Make sure that your program has proper documentation, including a prologue at the top, and anything else you think is important.

Explanation / Answer

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #::: ::: #::: This routine calculates the distance between two points (given the ::: #::: latitude/longitude of those points). It is being used to calculate ::: #::: the distance between two locations using GeoDataSource(TM) products ::: #::: ::: #::: Definitions: ::: #::: South latitudes are negative, east longitudes are positive ::: #::: ::: #::: Passed to function: ::: #::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) ::: #::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) ::: #::: unit = the unit you desire for results ::: #::: where: 'M' is statute miles (default) ::: #::: 'K' is kilometers ::: #::: 'N' is nautical miles ::: #::: ::: #::: Worldwide cities and other features databases with latitude longitude ::: #::: are available at http://www.geodatasource.com ::: #::: ::: #::: For enquiries, please contact sales@geodatasource.com ::: #::: ::: #::: Official Web site: http://www.geodatasource.com ::: #::: ::: #::: GeoDataSource.com (C) All Rights Reserved 2015 ::: #::: ::: #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: $pi = atan2(1,1) * 4; sub distance { my ($lat1, $lon1, $lat2, $lon2, $unit) = @_; my $theta = $lon1 - $lon2; my $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $dist = $dist * 60 * 1.1515; if ($unit eq "K") { $dist = $dist * 1.609344; } elsif ($unit eq "N") { $dist = $dist * 0.8684; } return ($dist); } #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #::: This function get the arccos function using arctan function ::: #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: sub acos { my ($rad) = @_; my $ret = atan2(sqrt(1 - $rad**2), $rad); return $ret; } #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #::: This function converts decimal degrees to radians ::: #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: sub deg2rad { my ($deg) = @_; return ($deg * $pi / 180); } #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #::: This function converts radians to decimal degrees ::: #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: sub rad2deg { my ($rad) = @_; return ($rad * 180 / $pi); } print distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles "; print distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers "; print distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles ";