Help! I'm trying to solve a quadratic equation

I keep getting the error :

term does not evaluate to a function taking 1 arguments

Heres the program. I highlighted the problem line

// Macros 3 - finding the root of a quadratic equation 
// ax^2 + bx + c  --> positive root = (-b+ (sqrt(b^2 - 4*a*c))) / 2a
// This program still has 2 flaws: it only solves one of the two roots of a quadratic equation
// and if the c part of the equation is 0 it will not solve the input quadratic

#include <iostream>
using namespace std;

double Abs(double Nbr);
double SquareRoot(double Nbr);
double Subfunction(double a, double b, double c);
inline double ROOT(double a, double b, double Nmbr) {return ((-b + Nmbr)/(2*a));}


// solves the (b^2 - 4*a*c) part of the equation to be put into square root solving functions 
double Subfunction(double a, double b, double c)
{
    double SubfunctionResult;
    SubfunctionResult = ((b*b)-4(a)(c));
        return SubfunctionResult;
}
// This is part of a function for solving a square root. 
// It works with SquareRoot()
double Abs(double Nbr)
{
    if( Nbr >= 0 )
        return Nbr;
    else
        return -Nbr;
}

// This is part of a function for solving a square root. 
// It works with Abs()
double SquareRoot(double Nbr)
{
    double Number = Nbr / 2;
    const double Tolerance = 1.0e-7;

    do Number = (Number + Nbr / Number) / 2;
    while( Abs(Number * Number - Nbr) > Tolerance);

    return Number;
}

int main()

{

    double x=1;
    double y=1;
    double z=1;
    double i;

    for (;;)
    {
        cout << "Enter the three parts to a quadratic equation in the form a^2 + b + c (0 to quit): ";
        cin >> x;
        cin >> y;
        cin >> z;
        if (x || y || z==0)
            break;
        cout << "You entered: " << x;
        cout << "You entered: " << y;
        cout << "You entered: " << z << endl;

        double SubfunctionResult1 = Subfunction(x,y,z);
        double Number = SubfunctionResult1;
        double Number1 = SquareRoot(Number);

        cout << "ROOT(" << x << " " << y << " " << z << " ): ";
        cout << ROOT(x,y,Number1) << "." << endl;

    }
    return 0;
}

Recommended Answers

All 4 Replies

This is programming, not algebra. You must show the multiplication operator, where you can omit it in mathematical expression.

SubfunctionResult = ((b*b)-4(a)(c));

must be

SubfunctionResult = ( b*b ) - (4 *a *c );

(All the parenthesis are actually not needed, but can add clarity for some.)

Also, in future posts, please put your code inside code tags, like:

[code]

your code goes here

[/code]

And, when referring to error messages, try to indicate which line in the code they refer to.

I am going to comment on the maths/computing interface.

You have two problems here, first is that although mathematically correct, the formula [tex]\frac{-b\pm\sqrt{b^2 - 4ac}}{ 2a}[/tex] is is awful as a computational base, since, as you partially discovered, if a or c are very small you get junk.

So you set
q=-\frac{1}{2}(b+sgn(b)\sqrt{b^2-4ac} )
and the roots are q/a and c/q. (sgn is the 1 if b>0 and -1 if b<0)

Note: additionally you need to check the condition that
b^2-4ac\ge0 or do the calculation in the complex plane.

Additionally, this still holds if the coefficients (a,b,c) are themselves complex or quaternions. (the condition is different).

There are many numerical discussions in scientific literature on
(a) approximating this (to speed up the calc at the cost of accuracy)

(b) the issue of machine accuracy. google scholar is helpful, but a membership of the ACM digital library is also going to help [and it is expensive].

(c) Numerical Recipes for X, have a discussion in chapter 5. http://www.nrbook.com/a/bookcpdf.php Note: I have given the link to the free to download pdf of the 2nd edition. You pay money for the third editions and NEVER put NR code in production code, as it is rarely robust.

p.s. I am not a proper mathematician. So if you talk to a REAL mathematician he/she will complain at my imprecision.

commented: Good comments on the math +7

A quadratic equation has two roots so put them in a struct and go from there like I did in this code snippet
http://www.daniweb.com/code/snippet980.html
ok it's C# but you will be surprised how much C++ is in there.
SubfunctionResult is what mathematicians call a discriminant. Check if it's > 0 ; = 0 or < 0 and act accordingly. See also the last part of my snippet.

Thanks to all.
I should be able to figure it out from here.
Especially ddanbe, your snippet was very handy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.