1

I have almost everything working except for solving for X in line 25 i keep getting an error saying " term does not evaluate to a function taking 1787 arguments" i had it giving me a 1 or a 0 but as i kept messing with it i lost where i was at and saved over the copy. still new to posting sorry if its hard to read

#include <stdio.h>
#include <math.h>

void quadratic_function()

{
    int         a,b,c;      // variables
    long int    result;     // my X in the quadractic function
    long int    y,x;            // the result
    long int    quadratic;

    printf("enter values for a,b,c\n");
    scanf("%i\n %i\n %i", &a,&b,&c);
    printf("A=%i  B=%i  C=%i\n", a,b,c); //Displays Variables

    y= pow(b, 2);
    result= (y)*-4*(a)*(c); // b^2-4ac
    printf("\n%li\n",result);


    if (result<0) 
        printf("Imaginary Number"); // if negative

    else (result>0);
        x=(-b/2*(a)) +- (sqrt(pow(b, 2)) (-4*(a)*(c))) / (2*(a)); 
        //solving for x

        printf("\n %li\n",x);

        a = a*x;
        b = b*x;
        quadratic=pow(a, 2)*(b)*(c);        // if positive

         //printf("Quadratic equation equal to %li",quadratic); // result

}

int main()
{
quadratic_function();
return 0;

}
techturtle
  • 2,519
  • 5
  • 28
  • 54
Pwoods
  • 105
  • 2
  • 4
  • 15
  • Please format your source code correctly. – Oswald Mar 14 '13 at 17:50
  • maybe the line `else (result>0);` should be `else if (result > 0) { ... rest of code }` ? – varnie Mar 14 '13 at 17:59
  • everything has worked except when I play with this line of code which I cant seem to get to work I've messed with it a lot to solve for x x=(-b/2*(a)) +- (sqrt(pow(b, 2)) (-4*(a)*(c))) / (2*(a)); – Pwoods Mar 14 '13 at 18:05
  • try the following: `x=(-b/2*(a)) +- (sqrt(pow(b, 2)) -4*(a)*(c)) / (2*(a));` – varnie Mar 14 '13 at 18:09
  • no problem. for the explanations please have a look at @jonhopkins answer below. – varnie Mar 14 '13 at 18:12

1 Answers1

2

The first thing I noticed is that you were trying to do the + and - portions of the quadratic equation at the same time. The equation

x = (-b +- sqrt(b^2 - 4ac)) / 2a

means the same as

x = (-b + sqrt(b^2 - 4ac)) / 2a AND x = (-b - sqrt(b^2 - 4ac)) / 2a

In other words, the equation has two answers if b^2 - 4ac is greater than 0, one answer if it is 0, and no answer if it is negative.

Another thing, the line else (result>0); doesn't really do anything. The rest of the code after that will execute even if you get b^2 - 4ac < 0

Finally, I wasn't entirely sure about your groupings or C++'s precedence with the negative sign, so I changed your parentheses around a bit.

y = pow(b, 2);
result = (y) - (4*a*c); // b^2-4ac
printf("\n%li\n", result);


if (result < 0) {
    printf("Imaginary Number"); // if negative

} else if (result == 0) {
    x = (-b) / (2 * a); // sqrt(0) = 0, so don't bother calculating it
    a = a*x;
    b = b*x;
    quadratic=pow(a, 2)*(b)*(c);
    printf("Quadratic equation equal to %li",quadratic); // result

} else if (result > 0) {

    // solve for (-b + sqrt(b^2 - 4ac)) / 2a
    x = ((-b) + sqrt(pow(b, 2) - (4 * a * c))) / (2 * a); 

    printf("\n %li\n",x);

    a = a*x;
    b = b*x;
    quadratic=pow(a, 2)*(b)*(c);
    printf("Quadratic equation equal to %li",quadratic); // result

    // do it again for (-b - sqrt(b^2 - 4ac)) / 2a
    x = ((-b) - sqrt(pow(b, 2) - (4 * a * c))) / (2 * a); 

    printf("\n %li\n",x);

    a = a*x;
    b = b*x;
    quadratic=pow(a, 2)*(b)*(c);
    printf("Quadratic equation equal to %li",quadratic);
}
jonhopkins
  • 3,844
  • 3
  • 27
  • 39