Page 1 of 1

Math++

Posted: Tue Apr 28, 2009 10:10 pm
by Bludklok
I was bored today and decided to write a small program to do my Algebra homework for me. This took no more then 10 minutes so its probably a little sloppy. :D

Code: Select all

#include <iostream>

using namespace std;

int main(void)
{
	while(1){
		int firstnumber = 0;
		int secondnumber = 0;
		int variable1 = 0;
		int variable2 = 0;

		cout << "Enter the first number:  ";
		cin >> firstnumber;

		system("cls");

		cout << "Enter the second number:  ";
		cin >> secondnumber;

		system("cls");

		while(variable1 + variable2 != firstnumber)
		{
			variable1++;
			variable2 = secondnumber / variable1;

			cout << "  ( x = " << variable1 << " )   ( x = " << variable2 << " )		WRONG" << endl;
		}
		cout << endl << endl;

		cout << endl << endl << "  ( x = " << variable1 << " )   ( x = " << variable2 << " )		CORRECT" << endl << endl << endl;

		system("pause");
		system("cls");
	}
	return 0;
}
I wrote this for factoring trinomials.

Basically without this code if I had the formula "m2 - 7m - 30" I would have to find a set of factors of "-30" that added up to "-7".

If i wanted to solve the equation with this code "7" would be the first number (enter the middle number first) and -30 would be the second number (enter the last number second).

This would be the output of the equation "m2 - 7m - 30".

Code: Select all

  
  ( x = 1 )   ( x = -30 )               WRONG
  ( x = 2 )   ( x = -15 )               WRONG
  ( x = 3 )   ( x = -10 )               WRONG


  ( x = 3 )   ( x = -10 )               CORRECT
Thats basically all there is to it.

Re: Math++

Posted: Tue Apr 28, 2009 11:30 pm
by Martyj
Nice, can it handle quadratics where the leading coefficient isn't 1? Like 3x^2 + 6x + 24

Re: Math++

Posted: Wed Apr 29, 2009 7:34 am
by Bludklok
Martyj wrote:Nice, can it handle quadratics where the leading coefficient isn't 1? Like 3x^2 + 6x + 24
This program isn't for solving the equation, just factoring out the trinomials. It wouldn't really matter if the first number had a coefficient anyways. ;)

EDIT: My bad I misunderstood your question. Ive just learned about quadratics today and no it doesnt handle them. Im probably going to make another program for quadratics because I have homework for it. :D

Re: Math++

Posted: Wed Apr 29, 2009 2:30 pm
by dandymcgee
I made a console program to do my physics homework (horizontal projectiles launched at an angle). I don't think there's anything wrong with doing it this way, because I actually learned a lot by getting the program to generate the correct output. :mrgreen:

Re: Math++

Posted: Wed Apr 29, 2009 2:47 pm
by MarauderIIC
I'll go ahead and move this over to code snippets :)

Re: Math++

Posted: Wed Apr 29, 2009 5:12 pm
by Altus
Back in the old days, we programmed our fancy calculators in BASIC or asm to do our homework for us. That and play bubble bobble. This might be ill-advised though, not that playing/programming games in class is a bad thing, it's just that whenever I do trigonometry now I get the uncontrollable urge to bust out my NES :shock:

Re: Math++

Posted: Wed Apr 29, 2009 6:42 pm
by Bludklok
MarauderIIC wrote:I'll go ahead and move this over to code snippets :)
Thank you. :)
dandymcgee wrote:I made a console program to do my physics homework (horizontal projectiles launched at an angle). I don't think there's anything wrong with doing it this way, because I actually learned a lot by getting the program to generate the correct output. :mrgreen:
Yea it actually makes homework semi-fun and builds problem solving skills as well as coding knowledge. I use C++ to solve my science conversion problems as well. :D

Re: Math++

Posted: Wed Apr 29, 2009 7:26 pm
by austech
Hmm, this makes me want to make programs to do my geometry homework :P

But it is true that it makes studying a little fun and learn. Maby now I wont fail my math tests :lol:

Re: Math++

Posted: Wed Apr 29, 2009 7:34 pm
by MarauderIIC
Be aware when doing this sort of thing that, well, to put it simply, floats are weird and can easily not do what you'd expect them to.

Re: Math++

Posted: Wed Apr 29, 2009 7:49 pm
by dandymcgee
MarauderIIC wrote:Be aware when doing this sort of thing that, well, to put it simply, floats are weird and can easily not do what you'd expect them to.
Yeah.. I found out the hard way that when you mix ints and floats in equations you get rather strange results if you don't cast first. :)