Page 1 of 1
My first program from scratch(calculator)
Posted: Tue Sep 08, 2009 10:08 pm
by fingerfry
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:
m = (y2 - y1)/(x2 - x1)
Here is the code:
Code: Select all
#include <math.h>
#include <stdio.h>
int main()
{
float fltcoordx1;
printf("Enter x1: \n");
scanf("%f", &fltcoordx1);
float fltcoordy1;
printf("Enter y1: \n");
scanf("%f", &fltcoordy1);
printf("Coordinate 1 is:\n (%f,%f)\n\n", fltcoordx1, fltcoordy1);
float fltcoordx2;
printf("Enter x2: \n");
scanf("%f", &fltcoordx2);
float fltcoordy2;
printf("Enter y2: \n");
scanf("%f", &fltcoordy2);
printf("Coordinate 2 is:\n (%f,%f)\n\n", fltcoordx2, fltcoordy2);
float equation1;
equation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1);
printf("The slope is: %f\n\n", equation1);
return 0;
}
It would be appreciated if you guys commented my code
Re: My first program from scratch(calculator)
Posted: Wed Sep 09, 2009 5:37 am
by Bludklok
Looks like it does the job for the purpose.
Although if you wanted to save memory you could not declare equation1 since you don't need that variable anywhere else in your code.
Example...
Code: Select all
//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.
Re: My first program from scratch(calculator)
Posted: Wed Sep 09, 2009 5:42 am
by K-Bal
If you are coding in C your code is fine. C++ offers some other architectures that make I/O safer and easier but nevermind if you are using C.
From a numerical point of view your formula is not very good because of floating point precision, but I guess that is not important for you here
Re: My first program from scratch(calculator)
Posted: Wed Sep 09, 2009 9:13 pm
by fingerfry
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
Code: Select all
#include <math.h>
#include <stdio.h>
int main()
{
float fltcoordx1;
printf("Enter x1: \n");
scanf("%f", &fltcoordx1);
float fltcoordy1;
printf("Enter y1: \n");
scanf("%f", &fltcoordy1);
printf("Coordinate 1 is:\n (%f,%f)\n\n", fltcoordx1, fltcoordy1);
float fltcoordx2;
printf("Enter x2: \n");
scanf("%f", &fltcoordx2);
float fltcoordy2;
printf("Enter y2: \n");
scanf("%f", &fltcoordy2);
printf("Coordinate 2 is:\n (%f,%f)\n\n", fltcoordx2, fltcoordy2);
float fltequation1;
fltequation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1);
printf("The slope is: %f\n\n", fltequation1);
float fltequation2;
fltequation2 = fltequation1 * (-1 * fltcoordx1) + (fltcoordy1);
printf("The slope-intercept form of this equation is: \n");
printf("y = %fx + (%f)\n\n", fltequation1, fltequation2);
return 0;
}
The only problem is that I don't know how to control how many decimal places are allowed :/
Re: My first program from scratch(calculator)
Posted: Wed Sep 09, 2009 10:04 pm
by XianForce
Well I guess that'd completely work if you never put in the coordinates for a vertical line... Then division by 0
Re: My first program from scratch(calculator)
Posted: Wed Sep 09, 2009 11:55 pm
by fingerfry
XianForce wrote:Well I guess that'd completely work if you never put in the coordinates for a vertical line... Then division by 0
Shit, forgot about that...
Code: Select all
#include <math.h>
#include <stdio.h>
int main()
{
float fltcoordx1;
printf("Enter x1: \n");
scanf("%f", &fltcoordx1);
float fltcoordy1;
printf("Enter y1: \n");
scanf("%f", &fltcoordy1);
printf("Coordinate 1 is:\n (%f,%f)\n\n", fltcoordx1, fltcoordy1);
float fltcoordx2;
printf("Enter x2: \n");
scanf("%f", &fltcoordx2);
float fltcoordy2;
printf("Enter y2: \n");
scanf("%f", &fltcoordy2);
printf("Coordinate 2 is:\n (%f,%f)\n\n", fltcoordx2, fltcoordy2);
if (fltcoordx2 == fltcoordx1)
printf("Cannot divide by zero. SLOPE IS UNDEFINED.\n\n");
float fltequation1;
fltequation1 = (fltcoordy2 - fltcoordy1) / (fltcoordx2 - fltcoordx1);
printf("The slope is: %f\n\n", fltequation1);
float fltequation2;
fltequation2 = fltequation1 * (-1 * fltcoordx1) + (fltcoordy1);
printf("The slope-intercept is: \n");
printf("y = %fx + (%f)\n\n", fltequation1, fltequation2);
return 0;
}
Fixed.
NOW, the only problem is that this prints to the screen if the domain of both coordinates are equal:
Cannot divide by zero. SLOPE IS UNDEFINED.
The slope is: 1.#INF00
The slope-intercept is:
y = 1.#INF00x + (-1.#INF00)
Press any key to continue . . .
Re: My first program from scratch(calculator)
Posted: Thu Sep 10, 2009 12:32 am
by fingerfry
Revised code, AGAIN. Added comments
Code: Select all
#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 . . . . . .
Re: My first program from scratch(calculator)
Posted: Thu Sep 10, 2009 2:45 pm
by RyanPridgeon
Since you seem to like this sort of stuff, you might like this site
http://www.projecteuler.net/
It has lots of maths problems that need to be programmed
Re: My first program from scratch(calculator)
Posted: Thu Sep 10, 2009 9:23 pm
by Bludklok
RyanPridgeon wrote:Since you seem to like this sort of stuff, you might like this site
http://www.projecteuler.net/
It has lots of maths problems that need to be programmed
Nice find. I know I'll be using that (I love writing cpp programs to solve math equations).
Re: My first program from scratch(calculator)
Posted: Fri Sep 11, 2009 10:19 pm
by fingerfry
Code: Select all
#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;
}
That solves our little, dividing by zero problem