Code:
#include <stdio.h>
int get_problem();
void get2_pt(double *x_onep, double *y_onep, double *x_twop, double *y_twop);
void get_pt_slope(double *x_onep, double *y_onep, double *slopep);
void slope_intcpt_from2_pt(double x_one, double y_one, double x_two, double y_two, double *slopep, double *y_intp);
double intcpt_from_pt_slope(double x_one, double y_one, double slope);
void display2_pt(double x_one, double y_one, double x_two, double y_two);
void display_pt_slope(double x_one, double y_one, double slope);
void display_slope_intcpt(double slope, double y_int);
int
main(void)
{
double x_one,
x_two,
y_one,
y_two,
slope,
y_int;
int problem;
char cont;
cont = 'Y';
while (cont == 'Y' || cont == 'y') {
problem = get_problem();
if (problem == 1) {
get2_pt(&x_one, &y_one, &x_two, &y_two);
slope_intcpt_from2_pt(x_one, y_one, x_two, y_two, &slope, &y_int);
display2_pt(x_one, y_one, x_two, y_two);
display_slope_intcpt(slope, y_int);
printf("\n\nDo another conversion? (Y/N): ");
scanf("%c", &cont);
}
else if (problem == 2) {
get_pt_slope(&x_one, &y_one, &slope);
y_int = intcpt_from_pt_slope(x_one, y_one, slope);
display_pt_slope(x_one, y_one, slope);
display_slope_intcpt(slope, y_int);
printf("\n\nDo another conversion? (Y/N): ");
scanf("%c", &cont);
}
else if (problem == 3) {
printf("Exiting Program");
cont = 'N';
}
else {
printf("Invalid response. Please try again.\n\n");
cont = 'Y';
}
}
return(0);
}
int
get_problem()
{
int problem;
printf("Select the form that you would like to convert to slope intercept form:");
printf("\n1) Two-point form (you know two points on the line)");
printf("\n2) Point-slope form (you know the line's slope and one point)");
printf("\n3) Exit");
printf("\n\n>> ");
scanf("%d", &problem);
return(problem);
}
void
get2_pt(double *x_onep, double *y_onep, double *x_twop, double *y_twop)
{
double one,
two;
printf("\nEnter the coordinates of the first point separated by one space: ");
scanf("%lf %lf", &one, &two);
*x_onep = one;
*y_onep = two;
printf("Enter the coordinates of the second point: ");
scanf("%lf %lf", &one, &two);
*x_twop = one;
*y_twop = two;
}
void
get_pt_slope(double *x_onep, double *y_onep, double *slopep)
{
double x,
y,
slope;
printf("\nEnter the coordinates of the known point separated by one space: ");
scanf("%lf %lf", &x, &y);
printf("Enter the slope of the line: ");
scanf("%lf", &slope);
*x_onep = x;
*y_onep = y;
*slopep = slope;
}
void
slope_intcpt_from2_pt(double x_one, double y_one, double x_two, double y_two, double *slopep, double *y_intp)
{
*slopep = ((y_two - y_one) / (x_two - x_one));
*y_intp = (y_one - (*slopep * x_one));
}
double
intcpt_from_pt_slope(double x_one, double y_one, double slope)
{
double y_int;
y_int = y_one - (slope * x_one);
return(y_int);
}
void
display2_pt(double x_one, double y_one, double x_two, double y_two)
{
printf("\nTwo-point form");
printf("\n (%.2f - %.2f)", y_two, y_one);
printf("\nm = -----------");
printf("\n (%.2f - %.2f)", x_two, x_one);
}
void
display_pt_slope(double x_one, double y_one, double slope)
{
printf("\nPoint-slope form");
printf("\n y - %.2f = %.2f(x - %.2f)", y_one, slope, x_one);
}
void
display_slope_intcpt(double slope, double y_int)
{
printf("\n\nSlope-intercept form");
printf("\n y = %.2fx - %.2f", slope, y_int);
}