A not so clever (you will see why later) cannoneer (someone who fires a cannon) has asked you for help. His job is to fire cannon balls from a cannon sitting on top of a cliff to hit a target on the ground. The cannon balls are fired horizontally and are aimed directly at the target. Assume there is no wind and that air resistance is negligible. This is shown in the diagram below:
As he has no understanding of the underlying physics, he can only blindly follow the "trial and error" method. Starting from a initial launching speed, he observes whether the cannon ball undershoots or overshoots the target. He will then adjust the launching speed by a fixed amount accordingly and tries again. To close in on the target, every time the direction of adjustment changes (i.e. from overshot to undershot or vice versa), he will halve the adjustment amount. This goes on until the cannon ball fells within a reasonable margin from the target or he has tried for 10 times and failed.
As an engineering or science undergrad, you can see a far easier way to do this without going out and launching cannon balls for the whole day. Write a program to show your friend that the result of every launch can be "predicted" precisely.
Your program should read from a data file named "target.txt". Each line of the file contains the following information (you can assume all values to be floating point):
Cliff_Height Target_Distance Initial_Launch_Speed Adjustment MarginFor example:
100.5 50 200 50 10tells you that the catapult is sitting on top of a cliff of 100.5 meters. The target is 50 meters from the cliff. The launching practice should starts with a initial launch speed of 200 meters/second, and adjusted with 50 meters/second until the cannon ball fells not more than 10 meters from the target.
As you should realize by now, this is simply to calculate the horizontal projectile movement (or parabolic movement). The two related formula are given below:
Vertical Distance = 0.5 * g * t^2 Horizontal Distance = LaunchSpeed * twhere, g is the gravity (taken as 10 m/s^-2), t is the flight time of the cannon ball, and Vertical Distance is the height of the cliff.
After the every launch, you should adjust the launch speed accordingly (increase if undershot, decrease if overshot) by the Adjustment amount. As mentioned earlier, whenever you encounter a change in adjustment direction, this Adjustment amount would be halved. Finally, whenever the cannon ball falls within the acceptable margin or you have tried for 10 times, the practice terminates.
100.5 50 200 50 10 100.5 1000 200 50 5 100.5 1000 200 50 .5Below is the output your program should produce. Observe the changes of the adjustment amount whenever the direction changes. Note that comments (indicated by "//") are included to explain the output. Your program should not contain these comments in the output.
Catapult at cliff of 100.50m //First setting Target at 50.00m from cliff Acceptable margin 10.00m Shooting Starts: Launch speed of 200.00 m/s hit distance of 896.66m Launch speed adjusted to 150.00 m/s Launch speed of 150.00 m/s hit distance of 672.50m Launch speed adjusted to 100.00 m/s Launch speed of 100.00 m/s hit distance of 448.33m Launch speed adjusted to 50.00 m/s Launch speed of 50.00 m/s hit distance of 224.17m Launch speed adjusted to 0.00 m/s Launch speed of 0.00 m/s hit distance of 0.00m Launch speed adjusted to 25.00 m/s //direction change Launch speed of 25.00 m/s hit distance of 112.08m Launch speed adjusted to 12.50 m/s //direction change Launch speed of 12.50 m/s hit distance of 56.04m Mission accomplished //within margin Catapult at cliff of 100.50m //Second Setting Target at 1000.00m from cliff Acceptable margin 5.00m Shooting Starts: Launch speed of 200.00 m/s hit distance of 896.66m Launch speed adjusted to 250.00 m/s Launch speed of 250.00 m/s hit distance of 1120.83m Launch speed adjusted to 225.00 m/s //direction change Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 200.00 m/s Launch speed of 200.00 m/s hit distance of 896.66m Launch speed adjusted to 212.50 m/s //direction change Launch speed of 212.50 m/s hit distance of 952.70m Launch speed adjusted to 225.00 m/s Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 218.75 m/s //direction change Launch speed of 218.75 m/s hit distance of 980.72m Launch speed adjusted to 221.88 m/s //direction change Launch speed of 221.88 m/s hit distance of 994.73m Launch speed adjusted to 225.00 m/s Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 223.44 m/s //direction change Launch speed of 223.44 m/s hit distance of 1001.74m Mission accomplished //within margin Catapult at cliff of 100.50m //Third Setting Target at 1000.00m from cliff Acceptable margin 0.50m Shooting Starts: Launch speed of 200.00 m/s hit distance of 896.66m Launch speed adjusted to 250.00 m/s Launch speed of 250.00 m/s hit distance of 1120.83m Launch speed adjusted to 225.00 m/s Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 200.00 m/s Launch speed of 200.00 m/s hit distance of 896.66m Launch speed adjusted to 212.50 m/s //direction change Launch speed of 212.50 m/s hit distance of 952.70m Launch speed adjusted to 225.00 m/s Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 218.75 m/s //direction change Launch speed of 218.75 m/s hit distance of 980.72m Launch speed adjusted to 221.88 m/s //direction change Launch speed of 221.88 m/s hit distance of 994.73m Launch speed adjusted to 225.00 m/s Launch speed of 225.00 m/s hit distance of 1008.74m Launch speed adjusted to 223.44 m/s //direction change Launch speed of 223.44 m/s hit distance of 1001.74m Launch speed adjusted to 221.88 m/s //10th try.
A total of 7 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.