views:

127

answers:

3

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 code 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! ;)

+5  A: 

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.

Paul Rubel
Um, the `print` with the "whatever" comment doesn't do what you think it does: run with warnings enabled to see the problem. You're dividing the return value of `print` by three, then discarding that result.
tchrist
Thanks @tchrist. See how useful it is to do things one step at a time. Fixed.
Paul Rubel
+4  A: 

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.

FM
+1 for "work iteratively." I'm always the most productive when I make a series of relatively small changes, rather than trying to do a lot at once.
friedo
+1  A: 

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. :)

brian d foy