Topical Information
This program works with more data to give you some more practice at
handling more variables. It will hopefully also give you an even better feel
for how much testing is necessary to verify a program's correctness.
Finally, it gives a refresher to some concepts from algebra.
Program Information
Write a program that helps the user calculate the midpoint of a line
segment in the 2D Cartesian plane. Try to make your results as readable as
possible.
As an example, you might have the program interaction look something like
(the parts in this color are typed by the
user):
$ ./midpoint.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!
$
Thought Provoking Questions
- How many variables are needed here? (Hint: There are two points being
input and a third point being output. How many values are needed to
'make' a 2D point?)
- Would it make any sense to have whole-valued point coordinates (as opposed
to decimal-valued point coordinates...)? That is, would it ever be
correct for you to assume that a point's coordinates would never have
fractional parts?
- What is the formula to calculate a midpoint? Compare/Contrast the x part of the formula and the y part. (I.E. How are they similar? How are they
different?)
- What happens if the input points are in different quadrants? In the same
quadrant? On an axis? At the origin? (I.E. Can your program ever go
wrong because of the points' locations?)
- How many different ways can you pick a pair of points from the Cartesian
plane: quadrant(s), axis/axes, origin? (In other words, how many tests
would it take to completely test your program?)
(You do not have to script
that many tests. But you should be able to calculate how many there
should be. That way when you are working in industry, you can more
thoroughly test your applications and run Micro-sloth out of business.
*chuckle* *grin*)
This assignment is (Level 1.5).
Options
Toward a More Natural Manner of Input
Students are complaining that their teacher makes them use standard
ordered pair notation for points in class, but your program makes them
enter points without the notation. They are getting frustrated and
some even confused.
Add (Level 1) to force the user to
enter the end points of the segment in normal, ordered-pair style (with
parenthesis surrounding the coordinates and a comma between them).
The program interaction might now look something like (the parts in
this color are typed by the user):
$ ./midpoint.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!
$
More Thought Provoking Questions
- To make the input style more natural, you'll need to make the user
type in the parentheses and the comma for typical Cartesian point
notation. What [data] type of variable(s) will this require?
- How many variables will you need to make the input style more
natural for the user? At minimum? At most?
- How can the parentheses and comma be placed right next to the numbers
like that? How can cin determine where
the number stops and the other stuff begins? Remember what happened
when you typed a symbol or letter at a numeric prompt before? It was chaos! Why
don't we see those problems here?
- What happens if the user doesn't type the parentheses or the comma?
What happens if they type some other symbols instead? Letters
instead? Digits instead? (Try input like: $4^5% and see what happens. Try 44 12x — what happens? Experiment...) (Do
not try to correct this behavior — just document
it.)
- What if everything they type is non-numeric (i.e. '&*$#^')? Remember what happened when you
typed a symbol or letter at a numeric prompt before? (Again, do
not try to correct this behavior — just document
it.)
Total Level Possible
If you did all above options, this lab could be worth as much as (Level 2.5).