Mathematical problem!

Hey guys! I am trying to develop a program that solves second grade equations.

If the equation is for example: x^2 + 2x + 3 = 0, the user has to enter the P and Q value. In this case that would be P = 2, and Q = -3.

Now here is the problem: For everybody who doesn't know, the PQ formula provides 2 values because with this formula you calculate two points of a curved line. My problem is that the only values i recieve back are two really high numbers. I recieve these same values no matter what i put as P or Q. Why is this happening?

If this helps, i am following this specific PQ formula for this program:

http://pavian.sourceforge.net/formula.gif

NOTE: I know that my code is really clunky, but i was lazy, ok?





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <math.h>
using namespace std;

int main() {

    int P;
    int Q;
    int equation;
    int rightside;
    int rightside2;
    int answer;
    int leftside;
    int answer2;
    int leftside2;
    int P1;
    int answer3;
    int answer4;

    cout  << "Welcome to the second grade equation calculator, made by Alexander Ensani TE14B" << endl;
    cout << "" << endl;

    cout << "What is the value of P?" << endl;
    cin  >> P;

    cout << "What is the value of Q?" << endl;
    cin >> Q;

    cout << "" << endl;

    if(Q > 0) {

            Q *= -1;

            } else if(Q < 0) {

                Q   -= Q*2;
            }

            if(Q > 0) {


    cout << "The equation: X = -";
    cout << P << " /2 " << " + SQR ( " << P << " /2 )^2 + " << Q << endl;
    } else

    {
      cout << "The equation: X = -";
    cout << P << " /2 " << " + SQR ( " << P << " /2 )^2 " << Q << endl;
    }
    cout << "" << endl;

     if(P1 < P1) {

            P1 *= -1;

            } else if(Q > 0) {

                P1   -= Q*P1;
            }

    P1 = P;

    rightside = P / 2;
    rightside2 = rightside * rightside;

    leftside2 = P1 / 2;

    answer = sqrt(rightside2);
    answer2 =  leftside + answer;

    answer4 = leftside - answer;

    cout <<  "Answer 1 is: ";
    cout << answer2 << endl;

    cout <<  "Answer 2 is: ";
    cout << answer4 << endl;



}


[/code]
Last edited on
i've only had a quick glance, but you have a LOT of uninitialised variables there (lines 7 to 18), which is bad.
I think you're using one of these variables in your code without setting it to something first.

edit: spotted some other stuff:
this:
if(P1 < P1)
makes no sense. (also, P1 seems to be one of those variables i mentioned where you're trying to use it without being set.
Last edited on
NOTE: I know that my code is really clunky, but i was lazy, ok?


No, it's really not ok. This may be the root of your problem. You typed up some code that is "clunky", poorly formatted and not commented. And then it doesn't work. So, you come to the forum to find out why the clunky code doesn't work.

Well, I'm too "lazy" to try to debug code for somebody who is too lazy to write non-clunky code in the first place, ok?.

I suspect that if you spend 10 minutes re-formatting the code into something that is less clunky (especially good indentation) and maybe add a comment here or there to let yourself and anyone else looking at the code know what it is doing, you might stumble upon your error in the process and not need to ask the question. When you are about to add a comment and you realize that the code doesn't do what the comment says it's doing, you probably found your bug.
What i ment by "clunky" was actually that i was using some very confusing integer names. And some that i don't use. That doesn't really affect the code's functionality if i have some integers i don't use. But wow, i didn't know there were some really stupid errors. Hahaha i'm sorry. And yes ofcourse, i am going to write it from scratch later.
what ide are you using? i know a lot of them have auto-format options. if you're using visual studio for example, i think it's off the Edit->Advanced menu.

you can also print out the values of your various variables at various stages of the program to see which ones look as expected and which ones don't.
I haven't looked at your code. I've used Dev C++ (not Orwell) for maybe 800 hours doing mathematics. After very exhaustive testing I believe that mathematical errors occur. Large floating point numbers sometimes appear where they do NOT belong!

I'm sorry if this is not directly relative to your situation, but it should be known that this serious situation exists in my opinion. I've probably wasted quite s few hours because of this problem.
Topic archived. No new replies allowed.