Programming Assignment 2: Equation of a Line Through Two Points CS 2073, Computer Programming with Engineering Applications Spring Semester, 1992 Write a Pascal program to display the equation of the straight line through two points on the graph of an equation. The points are obtained by reading values of the x coordinates of the two points. The corresponding y coordinates are obtained from the function. 1. The first thing your program should print is a line giving your name in the form: This program was written by.... 2. Each line of input will contain four two numbers representing the x coordinates X1, and X2 of two points on the graph of a function. The function used should be f(x) = x2-3x-2. Test your program with the following input data: 2.1 -2.1 0.5 3.75 1.0 3.0 1.0 -2.0 -2.0 4.0 2.0 2.0 0.0 4.0 -1.0 2.0 You can conveniently implement the function f by inserting the following code between the variable declaration part and the executable part of your program: function f(x: real): real; begin f := sqr(x) - 3.0*x - 2.0 end 3. The program should first print the coordinates of the two points in exactly the form Line through points: (2.10, -3.89), (-2.10, 8.71) Next (on a separate line) print out the equation of the line through the two points. The equation should be in the general form Y = m X + b, where m is the slope and b is the Y-intercept, both given with two decimal places. For full credit the equation should appear as you might expect to see it in a calculus book. (See Item 8 below.) 4. As with the first assignment, keep reading pairs of numbers and printing out equations of lines as long as you are not to the end-of-file. 5. After processing the last two points your program should terminate without an error message. 6. Your program should correctly handle any reasonable input. In particular it must deal with each of the following cases: · a line with a positive slope · a line with a negative slope · a horizontal line · two identical points (an error message) 7. Your program should be well-documented and formatted. You should use indentation, blank lines, internal comments, meaningful identifiers, annotated identifiers, a consistent style, and header comments as in Assignment 1. 8. One of the challenging parts of this assignment is to write out the equation of the line in a ªniceº form, as you might see it in a calculus book. For full credit, you should conform to the following rules: · Except for the cases below, use the form ªY = 1.20X + 2.20º. · For a horizontal line, use the form ªY = 2.05º. · If the slope is exactly 1, use the form ªY = X + 1.05º. · If the slope is exactly ±1, use the form ªY = -X + 22.25º. · If b is exactly 0, use the form ªY = -2.22Xº. · In case b is negative, you must not write ªY = 2.10X + - 3.30º. · For extra credit, if m or b is an integer, write them without a decimal point. 9. After you are satisfied with your program, produce a file with the final output as follows. Add the declaration ªoutfile:textº to your variable declaration section. Add another ªassignº and a ªrewriteº statement to the beginning of your program: assign(outfile, 'LINES.DAT'); rewrite(outfile); Change each ªwriteln(...º to ªwriteln(outfile,...º. Then get a listing of the Pascal source program and of the output file LINES.DAT. Hand in both. Note: for this assignment you do not have to hand in a disk with your program, but in case I have any questions, I may ask for such a disk. If you hand in output data that was not produced by your program, I will consider this as cheating, with a minimum punishment of a zero on the assignment. 2.1 -2.1 (2.10,-3.89),(-2.10,8.71) y = -3.00x + 2.41 0.5 3.75 (0.50,-3.25),(3.75,0.81) y = 1.25x -3.87 1.0 3.0 (1.00,-4.00),(3.00,-2.00) y = x - 5 1.0 -2.0 (1.00,-4.00),(-2.00,8.00) y = -4x -2.0 4.0 (-2.00,8.00),(4.00,2.00) y = -x + 6 2.0 2.0 (2.00,-4.00),(2.00,-4.00) Identical points 0.0 4.0 (0.00,-2.00),(4.00,2.00) y = x - 2 -1.0 2.0 (-1.00,2.00),(2.00,-4.00) y = -2x