Вопрос

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;

}
Это было полезно?

Решение

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);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top