/* Name: Neal R. Wagner, Course Instructor * Date: Due Feb 14, 1997 * Course: CS 1713 Section 02 * Subject: Print the equation of a line through * two points on the graph of an equation. * Algorithm: Read x-coordinates x1 and x2. * Calculate the corresp. y-coords y1 and y2 * on the graph of equation y = x^2 - 3x - 2. * Calculate the slope m and the y-intercept b. * Use m and b to print the line's equation. * Print integers without a decimal point. * Input: Keep reading pairs of x-coords up to zeros. * Output: Unless the x-coords are the same, print the * line in as in a calculus book. */ #include #include double f(double x); void printnum(double z); void printline(double x1, double y1, double x2, double y2); void main(void) { double x1, y1, x2, y2; /* coords of two points */ printf("Lines through x^2 - 3x - 2.\n"); for (;;) { scanf("%lf %lf", &x1, &x2); if (x1 == 0.0 && x2 == 0.0) break; y1 = f(x1); y2 = f(x2); printline(x1, y1, x2, y2); printf("\n\n"); } printf("Other lines:\n"); printline(2.0, 3.0, 2.0, 4.0); printf("\n"); printline(1.0, 0.0, 3.0, 0.0); printf("\n"); } /* f: function on which the points occur. */ double f(double x) { return x*x - 3.0*x -2; } /* print num: Print a double with two decimals unless */ /* it is an exact integer,*/ void printnum(double z) { if ( ((int) z) == z) printf("%1.0f", z); else printf("%3.2f", z); } /* printline: Print a line as in a calculus book. */ /* x1 and x2 are input x-coordinates of two points, */ /* with y1 and y2 the corresponding y-coordinates. */ void printline(double x1, double y1, double x2, double y2) { double m, b; printf("Line through points: "); printf("(%3.2f,%3.2f),(%3.2f,%3.2f)\n", x1, y1, x2, y2); if (x1 == x2) { if (y1 == y2) printf("Identical points. There is no line."); else { printf("Equation of line: X = "); printnum(x1); } return; } m = (y1 - y2)/(x1 - x2); b = -m*x1 + y1; printf("Equation of line: Y = "); /* handle case of no X term here, i.e., m == 0. */ /* This includes the case m == 0 && b == 0. */ if (m == 0.0) { printnum(b); return; } /* Print the X term, assuming m != 0. */ if (m == 1.0) printf("X "); else if (m == -1.0) printf("-X "); else { printnum(m); printf("X "); } /* Print constant term. Note: if b == 0, print */ /* nothing (works because m != 0 here). */ if (b < 0.0) { printf("- "); printnum(fabs(b)); } else if (b > 0.0) { printf("+ "); printnum(b); } } Lines through x^2 - 3x - 2. Line through points: (2.10,-3.89),(-2.10,8.71) Equation of line: Y = -3X + 2.41 Line through points: (0.50,-3.25),(3.75,0.81) Equation of line: Y = 1.25X - 3.88 Line through points: (1.00,-4.00),(3.00,-2.00) Equation of line: Y = X - 5 Line through points: (1.00,-4.00),(-2.00,8.00) Equation of line: Y = -4X Line through points: (-2.00,8.00),(4.00,2.00) Equation of line: Y = -X + 6 Line through points: (2.00,-4.00),(2.00,-4.00) Identical points. There is no line. Line through points: (0.00,-2.00),(4.00,2.00) Equation of line: Y = X - 2 Line through points: (-1.00,2.00),(2.00,-4.00) Equation of line: Y = -2X Line through points: (-1.00,2.00),(4.00,2.00) Equation of line: Y = 2 Line through points: (-0.50,-0.25),(3.50,-0.25) Equation of line: Y = -0.25 Other lines: Line through points: (2.00,3.00),(2.00,4.00) Equation of line: X = 2 Line through points: (1.00,0.00),(3.00,0.00) Equation of line: Y = 0next1.cs.utsa.edu>