How can I calculate the distance between two points in Perl?
I need help with a homework assignment for my beginner computer science class. I am completely lost!
I need to write a program in Perl that will calculate the distance between 2 points with three values (x,y,z) by the given formula by my professor. the program must do the following:
- prompt for 'c' to continue 'q' to quit
- prompt for the x, y, z coordinate indivually of the first set
- prompt for the x, y, z coordinate indivually of the second set
- compute the distance between the distance between the two sets and then display the value and quit.
This is what I've done so far:
Psuedo c开发者_运维知识库ode outline the above
IF THEN process outline for the c to continue and q to quit part Found a sqrt equation for computing the distance
Rather than getting code, I'm really looking for tips on where to start here. Do I start by defining my variables? Any tips or direction on a first outline would be really appreciated! ;)
You've got three main problems and one hopefully trivial one.
If I was doing this I'd do the following:
1) Make sure my installation was working, there's nothing worse than thinking its a code problem and finding out your install is broken. Can you run hello world? (This should be the trivial one.)
print "Hello World\n";
2) Experiment with the math, getting the distance computation working. Start with hard-coded values:
$x = 1;
$y = 2;
$z = 3;
print (($x + $y ) / 3); #whatever
3) Figuring out how to read the input. You might want to take a look at the learn perl website
4) Put it together.
Start by using good practices. Include this at the top of your Perl script.
use strict;
use warnings;
Another good practice is to learn how to define and call subroutines, including how to pass arguments into them and retrieve their return values. You want each component of your program to be in a subroutine that performs one task well. This may seem like unnecessary overhead for a simple first assignment, but it will be well worth the effort, especially when things are not working correctly.
Then try to figure out what some of your subroutines might be, and start wiring them together so that they will call each other in the right sequence. Don't worry about details or getting the subroutines to do anything interesting. Just put a few of the main components in place. Here's a start:
use strict;
use warnings;
main();
sub main {
print "Running main().\n";
my $reply = get_user_input("Enter something.");
print $reply, "\n";
}
sub get_user_input {
print "Running get_user_input().\n";
my $message = shift @_;
print $message, "\n";
return 1234;
}
Note how the subroutines include various print
statements. These won't necessarily be in the final program, but they are useful as you are developing it -- essentially providing extra confirmation that each step is working as you expect.
Which takes use to another good practice: work iteratively. Get something simple running. Make a small adjustment. Run the program again. Confirm that it's working (initially just by printing stuff). Make another small adjustment. Run again. Etc.
As with any program, start at the beginning.
Write a program to print a prompt to the user.
Modify that program to take input from the user.
Modify that program to check the input from the user.
That is, do each baby step until you've satisfied all of the requirements.
If you need to learn Perl first, start with Learning Perl. You should only need the first four chapters to complete this simple program. :)
精彩评论