The goal of this lab is to gain more comfort with C++ functions
and arrays. We will practice using arrays to process pairs of points along a path and display some basics statistics. A empty version the program will appear in your
cs35/labs/01 directory when you run update35. The
program handin35 will only submit files in this directory. In
later labs you can work with a partner, but for this lab you should
work on your own.
For this lab you will prompt the user to enter a series of point coordinates along a path.
This program asks a user to enter a path of points using x,y coordinates and displays some statistics about the path This process is repeated for multiple paths. How many paths would you like to enter: 2 Path #1 Enter x,y pairs separated by spaces Enter -1 -1 to quit > 0 0 > 0 1 > 1 0 > -1 -1 Total distance is 2.41421 over 2 legs Average leg length = 1.20711 Distance from start to end is 1 Path #2 Enter x,y pairs separated by spaces Enter -1 -1 to quit > 1 1 > 2 3 > 4 5 > 4.5 3.2 > -1 -1 Total distance is 6.93265 over 3 legs Average leg length = 2.31088 Distance from start to end is 4.13401
Add a function void getPath() which prompts the user to enter points separated by spaces. The user can use -1 -1 to indicate they are done entering points. Do not treat -1 -1 as a point on the path. Display the total distance of the path, by adding up the distances between each pair of adjacent point on the path. Display the average length of a leg on the path (two successive points). Finally display the distance from the first point on the path to the last point. Note that you will need more than one array to store points. To store a single point, use the following array declaration: float pt[2];
Once getPath is working for a single path, add a function of your own design that will ask the user how many paths she/he would like to enter and then repeatedly loops to get this many paths. This function should return a valid positive integer. You may assume that the user will type in an integer, but you should confirm that it is positive. Incorporate this function into your main function such that the user can enter multiple paths as shown above.
Instead of typing in a bunch of numbers each time to test your program, you can save some sample data in a file, e.g., test1.txt containing only the input values:
1 0 0 0 1 1 0 -1 -1Then you can use input redirection to have your program read input from a file, e.g., ./path < test1.txt. Try it. This may be how your instructor tests your submisison, so it is a good idea to try it before I try it.
Use an consistent indenting style you are comfortable with.
Avoid lines longer than 80 characters. Recall C++ doesn't care about whitespace in most contexts, so you can break up long cout statements over multiple lines.