This lab should give you some practice writing your own functions.
Write a program that helps the user calculate the midpoint of a line segment in the 2D Cartesian plane. (I know, I know... been there, done that. *phbbt* Keep reading, smarty!)
Try to make your results as readable as possible.
Use a function to calculate the midpoint between two 1D coordinates. The inputs (arguments) for this function should be the coordinates of the two points. The output (return value) for the function should be the calculated midpoint. This function will need to be called twice — once for the x coordinates and once for the y coordinates.
As an example, you might have the program interaction look something like (the parts in this coloration are typed by the user):
$ ./midpointfunc.out Welcome to the 2D Midpoint Program!!! What is the first end-point? (3.4, 12.2) What is the second end-point? (13.4, 12.2) Thank you!! Calculating... Done. The midpoint of the line segment between (3.4, 12.2) and (13.4, 12.2) is (8.4, 12.2). Thank you for using the TMP!! Endeavor to have a voracious day! $
How many arguments does your function take? What are the data types of the arguments?
What did you name the arguments to your function? Do the names matter to the compiler? Do the names matter at all?
How many values does the function return? What is the returned value's data type?
How many times do you call this function?
IF you needed to for some reason, could you use this function when working with 3D points?
(Do NOT alter your program in any way!!! Just answer the question! ...*sweet, oddly-juxtaposed smile*)
This assignment is (Level 3).
Add (Level 1) to have your input format be more flexible. Allow the user to leave off one or both parentheses or to leave out the comma between the two coordinates. If they do, don't worry about it, but print a little reminder message to tell them what notation they messed up on.
So, to enter the point '(3,-4)', the user should be able to enter any of: '(3,-4)', '3,-4)', '(3,-4', '(3 -4)', '3,-4', '3 -4)', '(3 -4', or even '3 -4'. Only the first would be quiet, however, the others would generate 1, 2, or even 3 warning/reminder messages about missing notation.
Add (Level 1) to have your input validate the user's entry format more forcefully. For this particular input, that is made tricky by the optionality of the '(', ',', and ')' parts of the traditional notation. However, if the user has entered what should be a number, make sure it is numeric. Also make sure the user only uses '(', ',', and ')' in those places — when they are entered. All the while, make sure that they can still place spaces between all parts of the point — Freedom of Input Act of 1987, remember? *grin* (Recall that, although the ws manipulator will eat any upcoming whitespace in the buffer, it treats '\n' no differently than any of the other spacing characters...and thus can cross over the user's original Enter-y.)
Add (Level 3) to use other functions to break your program into more manageable pieces. I'd recommend ones for printing a 2D point in normal notation, printing the results, and ...um...hmm... Well, you're already using a function for calculating the midpoint of two points, aren't you? *grin*
I'll throw in another (Level 1) to create A generic message printing function and use it [at least] to display the greeting and closing messages.
Add (Level 1) for re-using your point library from the options of "Point(in), out = Point". (If you didn't do this project, you can still get this Level — plus 0.5 more — by creating the library it describes.)
Add (Level 2) for using encapsulation and re-use (and maybe overloading) to provide specialized functions for 1D, 2D, and 3D midpoint calculations. (These functions should re-use your current function!!!)
(This will NOT change the purpose of the program to deal with other dimensionalities! This only provides functions that could deal with those other possibilities ...should the need arise at some point!!!)
Add (Level 1) to use a single function to input a 2D point. It should have no inputs and two outputs (the two coordinates). (This function should go in your point library if you did that option above. Don't worry, the levels will add up...)
If you combine this option with the input validation option above, you can add another (Level 1) to have this function manipulate cin's idea of failure when any of the good entry practices outlined above are violated so that the calling part of the program can use cin as a guide as to whether entry was or was not successful.
If you did all above options, this lab could be worth as much as (Level 14.5).