CS35 Lab1: Introduction to C++

Due 11:59pm Wednesday, January 26th

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.

Introduction

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

Program Requirements
Develop your program incrementally. Create a main function first. This function does not need to have much content. Simply displaying instructions with cout would suffice for a start. Next design, implement, and test a function void getPoint(float pt[]) that reads in a point from the user. Note you do not need to return the point because you will be modifying the array instead and the changes will be visible outside of the getPoint function. Try calling you function from main and displaying the result before adding additional functions. You should add a function float distance(float p1[], float p2[]) to compute the distance between two points using the formula sqrt(dx*dx+dy*dy) where dx and dy are the differences in x and y coordinates of the points, respectively. To use sqrt you will need to add #include <cmath> to the top of your program.

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 -1
Then 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.
Style
All functions should have a descriptive comment at the beginning describing the input parameter, the return value (if any) and a brief non-technical description of the purpose of the function.

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.

Submit
Once you are satisfied with your code, hand it in by typing handin35. This will copy the code from your cs35/labs/01 to my grading directory. You may run handin35 as many times as you like, and only the most recent submission will be recorded.