My first program from scratch(calculator)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
fingerfry
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Tue Sep 01, 2009 6:31 pm
Favorite Gaming Platforms: Xbox 360, PC, Gameboy Advance SP
Location: Palm Desert, California
Contact:

My first program from scratch(calculator)

Post 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 :)
Image
User avatar
Bludklok
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Tue Apr 14, 2009 1:31 am
Current Project: EnigmaCore
Favorite Gaming Platforms: PC, N64, Playstation1, Playstation2
Programming Language of Choice: C++
Location: New Jersey
Contact:

Re: My first program from scratch(calculator)

Post 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.
Youtube
Website
Current project: Enigma Core
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: My first program from scratch(calculator)

Post 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 ;)
fingerfry
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Tue Sep 01, 2009 6:31 pm
Favorite Gaming Platforms: Xbox 360, PC, Gameboy Advance SP
Location: Palm Desert, California
Contact:

Re: My first program from scratch(calculator)

Post 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 :mrgreen:

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 :/
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: My first program from scratch(calculator)

Post by XianForce »

Well I guess that'd completely work if you never put in the coordinates for a vertical line... Then division by 0 :shock:
fingerfry
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Tue Sep 01, 2009 6:31 pm
Favorite Gaming Platforms: Xbox 360, PC, Gameboy Advance SP
Location: Palm Desert, California
Contact:

Re: My first program from scratch(calculator)

Post 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 :shock:
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 . . .
Image
fingerfry
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Tue Sep 01, 2009 6:31 pm
Favorite Gaming Platforms: Xbox 360, PC, Gameboy Advance SP
Location: Palm Desert, California
Contact:

Re: My first program from scratch(calculator)

Post 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 :shock: . . . . . . maybe . . . . . .
Image
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: My first program from scratch(calculator)

Post 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 :)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Bludklok
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Tue Apr 14, 2009 1:31 am
Current Project: EnigmaCore
Favorite Gaming Platforms: PC, N64, Playstation1, Playstation2
Programming Language of Choice: C++
Location: New Jersey
Contact:

Re: My first program from scratch(calculator)

Post 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).
Youtube
Website
Current project: Enigma Core
fingerfry
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Tue Sep 01, 2009 6:31 pm
Favorite Gaming Platforms: Xbox 360, PC, Gameboy Advance SP
Location: Palm Desert, California
Contact:

Re: My first program from scratch(calculator)

Post 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 8-)
Image
Post Reply