Okay, so I'm a fairly new programmer. I realize that this is no great feat in the programming world, but the important thing is that I took another step towards betterment of my programming skills
As I returned home from another day of school, I finished my homework and just thought about a simple program I could write. I decided I wanted to try to make a program that would calculate the slope of a line(it was in my math homework). I didn't think it would be too hard for me. I made it so you enter the coordinates one at a time. You enter X1, then Y1, then X2, then Y2. It calculates the slope using this equation:
//float equation1; This variable is unneeded.
//equation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1); This is unneeded as well.
printf("The slope is: %f\n\n", (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1));
// Now your program will print out whatever variable the equation gives. Woohoo you saved yourself 4 bytes of data. :)
If you want some very good C++ video tutorials go here.
I have revised my program with a few more lines of code. Now you can enter the coordinates and it will give you the answer in slope intercept form, instead of only giving you the slope
#include <math.h>
#include <stdio.h>
int main()
{
float fltcoordx1; //enter X1 coordinate
printf("Enter x1: \n");
scanf("%f", &fltcoordx1);
float fltcoordy1; //enter Y1 coordinate
printf("Enter y1: \n");
scanf("%f", &fltcoordy1);
printf("Coordinate 1 is:\n (%f,%f)\n\n", fltcoordx1, fltcoordy1);
float fltcoordx2; //enter X2 coordinate
printf("Enter x2: \n");
scanf("%f", &fltcoordx2);
float fltcoordy2; //enter Y2 coordinate
printf("Enter y2: \n");
scanf("%f", &fltcoordy2);
printf("Coordinate 2 is:\n (%f,%f)\n\n", fltcoordx2, fltcoordy2);
if (fltcoordx2 == fltcoordx1)//if 0 is the denominator display message
printf("Cannot divide by zero. SLOPE IS UNDEFINED.\n\n");
float fltequation1; //do the math to determine the slope
fltequation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1);
printf("The slope is: %f\n\n", fltequation1);
float fltequation2; //do the math to put the slope and (X1,Y1) coordinates in standard form
fltequation2 = fltequation1 * (-1 * fltcoordx1) + (fltcoordy1);
printf("The slope-intercept form is: \n"); //give slope-intecept form of equation
printf("y = %fx + (%f)\n\n", fltequation1, fltequation2);
float flt_othr_sde_of_equa1; //declare what the variable is when you change from the right to thew left side
flt_othr_sde_of_equa1 = -1 * fltequation1; //change sign so it can switch sides
float flt_othr_sde_of_equa2; //declare what the variable is when you change from the right to thew left side
flt_othr_sde_of_equa2 = -1 * fltequation2; //change sign so it can switch sides
printf("The standard form is: \n"); //give standard form of equation
printf("%fx + y = %f\n\n", flt_othr_sde_of_equa1, fltequation2);
printf("The general form is: \n"); //give general form of equation
printf("%fx + y + (%f) = 0\n\n", flt_othr_sde_of_equa1, flt_othr_sde_of_equa2);
return 0;
}
Now it will display General Form and Standard Form. I think I'm going to extend this to quadratics . . . . . . maybe . . . . . .
#include <math.h>
#include <stdio.h>
int main()
{
float fltcoordx1; //enter X1 coordinate
printf("Enter x1: \n");
scanf("%f", &fltcoordx1);
float fltcoordy1; //enter Y1 coordinate
printf("Enter y1: \n");
scanf("%f", &fltcoordy1);
printf("Coordinate 1 is:\n (%f,%f)\n\n", fltcoordx1, fltcoordy1);
float fltcoordx2; //enter X2 coordinate
printf("Enter x2: \n");
scanf("%f", &fltcoordx2);
float fltcoordy2; //enter Y2 coordinate
printf("Enter y2: \n");
scanf("%f", &fltcoordy2);
printf("Coordinate 2 is:\n (%f,%f)\n\n", fltcoordx2, fltcoordy2);
if (fltcoordx2 == fltcoordx1)//if 0 is the denominator display message and stop program
printf("Cannot divide by zero. SLOPE IS UNDEFINED.\n\n");
else if (fltcoordx2 != fltcoordx1)
{
float fltequation1; //do the math to determine the slope
fltequation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1);
printf("The slope is: %f\n\n", fltequation1);
float fltequation2; //do the math to put the slope and (X1,Y1) coordinates in standard form
fltequation2 = fltequation1 * (-1 * fltcoordx1) + (fltcoordy1);
printf("The slope-intercept form is: \n"); //give slope-intecept form of equation
printf("y = %fx + (%f)\n\n", fltequation1, fltequation2);
float flt_othr_sde_of_equa1; //declare what the variable is when you change from the right to thew left side
flt_othr_sde_of_equa1 = -1 * fltequation1; //change sign so it can switch sides
float flt_othr_sde_of_equa2; //declare what the variable is when you change from the right to thew left side
flt_othr_sde_of_equa2 = -1 * fltequation2; //change sign so it can switch sides
printf("The standard form is: \n"); //give standard form of equation
printf("%fx + y = %f\n\n", flt_othr_sde_of_equa1, fltequation2);
printf("The general form is: \n"); //give general form of equation
printf("%fx + y + (%f) = 0\n\n", flt_othr_sde_of_equa1, flt_othr_sde_of_equa2);
}
return 0;
}