-1

I am doing my first code by myself. Everything up till now has been copy paste, make some changes to examples. I think I am heading in the right direction, but not sure.

#include <iostream>
#include <cmath>
using namespace std;
int main ( )

{
    int a, b, c, x1, x2;

        cout << "Please enter an integer value for a " << endl;
            cin >> a;
        cout << "Please enter an integer value for b " << endl;
            cin >> b;
        cout << "Please enter an integer value for b " << endl;
            cin >> c;

        cout << "           The Quadratic Formula is: \n";
        cout << "           X = (-b +- sqrt ((b * b) - 4 * a * c)) \ (2 * a)\n";
        cout << " \n";

        x1 = sqrt((b * b)- 4 * a *c);
        x2 = -sqrt((b * b) - 4 * a * c);

        if (x1 < 0) 
        {
            //use 1, 2, 3 
            cout << "There are no real roots 1";
            cout << x1;
        }

            else if (x1 == 0)
            {
                //use 4, 12, 9
                cout << "There is one real root ";
                cout << x1;
            }

                else 
                {
                    //use 2, 11, 5
                    cout << "There are two real roots ";
                    cout << x1 << ", " << x2;
                }

    return 0;
}

If I enter three numbers when asked, it should calculate whether it has no real squares, one real square, or two real squares, and tell you the results. It seems to do that, but I am not sure if I got the math down. I did find some numbers that give individually expected results. I am not sure if I have put the correct math down, or if this could be done easier? Thanks ahead.

  • 1
    "up till now has been copy paste" - That's a really *bad* way to learn C++. The language is too complex to make trial-and-error a useful approach. Read a few [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead (and/or attend some classes). C++ takes *years* to learn. You can't just "hack it". – Jesper Juhl Jun 02 '19 at 15:27
  • You shouldn't need to fire `sqrt` anywhere if all you're doing is testing real vs. imaginary root counts. It offers nothing. – WhozCraig Jun 02 '19 at 15:27
  • 3
    It's unclear whether your question is whether the formulas you are using are correct, or whether you are not getting the expected results. If you're not sure about what the correct formulas are, then stackoverflow is not the right web site for your question. This is not a mathematics-oriented web site. If you believe your formulas are correct, but the results are not the expected results, then just use your debugger to run this code one line at a time, inspect the values of all variables as they compute, and determine when the values begin to diverge, and why. This is what a debugger is for. – Sam Varshavchik Jun 02 '19 at 15:28
  • I am actually in a course right now. This is just the first "real project" we've had. – Andrew Pannelli Jun 02 '19 at 15:31
  • I thought the sqrt was part of the formula? As in, what follows is what is under the square root symbol in the quadratic formula. – Andrew Pannelli Jun 02 '19 at 15:33
  • my question was partly was the formula (math) correct, and also does the code look logic I guess? I don't get any erros, and I get answers, that seem to be correct, just kind of double checking if it looks like something logical? – Andrew Pannelli Jun 02 '19 at 15:34
  • Well, the code logic is definitely wrong. It is very unlikely that you will ever get a negative value returned from `sqrt()`, so checking "if (x1 < 0)" does not really accomplish anything useful. And whether "sqrt was part of a formula", or not, is not a C++ question. It is a mathematics question, that has nothing to do with C++. – Sam Varshavchik Jun 02 '19 at 15:36
  • Cool. So guess my next step is the Math lab at school to make sure I am calculating the math correctly then. – Andrew Pannelli Jun 02 '19 at 15:39
  • Yes. Your next step is then to determine what exactly you need to calculate. Until you figure out the right logic to implement it in code, noone can help you. Then, if you have trouble implementing it in C++, use your debugger to run your code one line at a time, inspect the values of all variables as they change, and analyze its actual logic. Knowing how to effectively use a debugger is a mandatory skill for every C++ developer. No exceptions. With a debugger, one can usually figure out bugs in their own code in minutes, instead of posting a question and waiting days to see if anyone answers. – Sam Varshavchik Jun 02 '19 at 15:42

1 Answers1

0

You only need to look at the discriminant of a quadratic function to see the type of its roots. The discriminant is the part under the square root in the quadratic equation:

quadratic equation

If the discriminant is negative, the roots are imaginary. If it's positive, two distinct real roots exist. And if it's zero, only one root of size -b/(2*a) exists.

So you could do something like this:

float tol = 1e-6;
float discriminant = b*b -4*a*c;
if ((discriminant < 0.0 + tol) && (discriminant > 0.0 - tol)) {
  std::cout << "one real root.\n";
} else if (discriminant < 0.0) {
  std::cout << "imaginary roots.\n";
} else {
  std::cout << "real roots.\n";
}

Also, I wouldn't use integers in mathematical expressions, they don't allow for decimals, and so you'll get unexpected results.

The image is from Wikipedia.

borizzzzz
  • 620
  • 1
  • 6
  • 17