Error in the roots of Bhaskara

Asked

Viewed 54 times

0

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

void func(double x1, double x2, double x3, double *delta, double *r1, double *r2);

int main(){
    FILE *file;
    file=fopen("log.txt","w");  
    double x1, x2, x3, delta, r1=0, r2=0;
    printf("entre com as tres raizes;\n");
    scanf("%lf %lf %lf", &x1, &x2, &x3);
    func(x1, x2, x3, &delta, &r1, &r2);
    printf("delta = %.1lf \nraiz 1 = %.1lf \nraiz 2 = %.1lf\n", delta, r1, r2); 
    fprintf(file,"delta = %.2lf\nRaiz 1 = %.2lf\nRaiz 2 = %.2lf", delta, r1, r2);
    fclose(file);

}
void func(double x1, double x2, double x3, double *delta, double *r1, double *r2){
    *delta=pow(x2, 2)-4*(x1)*(x3);
    if(delta>0)
        printf("raizes distintas\n");
    else if(delta=0)
        printf("ha so uma raiz real\n");
    else
        printf("nao ha raiz real\n");
    *r1=-x2+sqrt(*delta)/2*x1;
    *r2=-x2-sqrt(*delta)/2*x1;
}

The results from "R1" and R2" don’t add up if we run the tab. For example, for a=1, b=-13, c=22, R1=17.5 and R2=8.5. It was supposed to be giving 11 for R1 and 2 for R2. Thank you! Note. Bhaskara’s formula.

  • 1

    Hello, I am reading the code and I see that the bhaskara formula is not correct. I think it should look like this: R1=(-x2+sqrt(delta))/2X1

  • Hello, that’s right! Thank you!

1 answer

0

The error is in precedence, the way it is doing raiz1 behaves like this:

inserir a descrição da imagem aqui

  • Wow! That’s right. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.