This assignment will give you practice with variables, arithmetic statements, functions, and conditions, as well as an introduction to programming design.
You are to write a program which computes and prints out the equation for a line, given two points on that line, (X1, Y1) and (X2, Y2). It should also print out the distance between those two points.
Your program should prompt the user (in an informative manner) for these two points, and print the equation of the line formed by the two points and the distance between the two points in an informative manner as well.
As you may (or may not) remember from geometry, any two points determine a line. We will call those points (X1, Y1) and (X2, Y2).
Any line in two-dimensional space may be defined by the following equation:
y = mx + b
where
This can be computed from the following formulas:
b = Y1 - mX1
Y1 - Y2
m = -------X1 - X2
The distance between two points (X1, Y1)
and (X2, Y2) may be computed with the following formula:
__________________
Ö
(X1 - X2)2 + (Y1 - Y2)2
Note that the above formula will not work for the case where X1 and X2 are identical. In that case, the denominator of the slope formula will be 0, causing an error.
To prevent this, you are to use a simple if statement to detect this problem before the slope is computed. If X1 and X2 are identical, then the following should happen:
1. The message Slope is infinite, no y-intercept should be printed.
2.
The program should exit. This can be done by calling the exit(0)
function.
Before you write any code, there are a number of things that you must consider:
You should start by developing a pseudocode version of this program, detailing the basic steps it will include (there will not be very many in this program). In particular, determine the inputs that you will have to ask for, and the order that you will perform the computations.
Also think carefully about what type each variable should be, and keep C's rules for operator precedence in mind when writing code to implement the formulas given above.
Decide what the input prompts and the output should look like. These should be as informative as possible. Think about it from the user's point of view -- if you were using this program for the first time, without knowing anything about how it was written, how would you like it to look? This is an important part of your grade.
As with many of the programs that you write, you may find it easier to build and test it incrementally -- that is, to write it and test it one step at a time. This will make it easier to isolate and correct any errors. In particular, I suggest that you not add the code to handle illegal points until after you have everything else working.
In any case, make sure that you test your code thoroughly, trying enough test cases to be sure it works correctly. Make sure that you test at least one case where the points are illegal!
The program you turn in should be well documented. You do not have to document each line of the program, but the documentation should explain what each major section of your code does, and (if appropriate) how it does it. The pseudocode that you wrote as part of the design process is a good start for what your final documentation should look like.
In addition, include a header at the top, giving your name and a brief description of what the program does. Also make sure that your variable names are descriptive (this is also part of good documentation!). This is an important part of your grade.
john@cis.ysu.edu