C: Delta, X1 and x2 result always giving 0.000000

Asked

Viewed 633 times

0

I’m learning C in college, and I’m stuck with this algorithm. I can’t find the error.
It can differentiate whether delta is greater or less than zero, but always prints delta, X1 and x2 as 0.000000

   /*
    Name: Bhaskara
    Date: 10/08/17 19:30
    Description: Escreva um algoritmo que calcule a formula de bhaskara. 
    O Algorismo deve atender a condição do delta:
    D = 0, a equação tem duas raizes iguais
    D > 0, a equação tem duas raizes diferentes
    D < 0, a equação tem duas raizes reais
*/

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

main() {

    setlocale(LC_ALL, "Portuguese");

    float a, b, c, delt, x1, x2;


    printf("Digite o valor de a: \n");
    scanf("%f", &a);

    printf("Digite o valor de b: \n");
    scanf("%f", &b);

    printf("Digite o valor de c: \n");
    scanf("%f", &c);

    delt = b*b -4*a*c;
    printf("Delta = %f \n", &delt);

    if (delt >= 0) {

        x1 = (-b + sqrt(delt))/(2*a);
        x2 = (-b - sqrt(delt))/(2*a);
        printf("x1 = %f \n", &x1);
        printf("x2 = %f \n", &x2);
    }

        else {
        printf("A equação não tem raizes. \n"); 
        }
}

3 answers

1

You are printing the memory address of the variables &delt, &x1, &x2. The right thing would just be delt, x1, x2

1

& before a variable indicates that you are searching for its address and not the content, so that at the time you receive a data you use the variable &variable because it stores that data in the address of the variable, more when printing you should call only the variable because at that moment you want the content.

printf("Delta = %f \n", &delt); =fica=> printf("Delta = %f \n", delt);

and the same applies to other prints

If you want to better understand the content I tell you to study pointers, will be of great use.

0


To understand your error you will need an explanation about the operator & (í commercial) or (and commercial). This operator in C language means: memory address of...

Therefore, if you put this operator in front of some variable it will return the address that the variable is allocated in RAM.

As you know, the variables are allocated in memory and therefore have an address where it is used to access this content.

In basic programming of C language, without use of pointers, to access the contents of a variable just write the name of this variable and compiler takes care of finding its address and bring or write something inside the variable.

In the case of the function scanf she needs to know the address of the memory region of the variable to be able to put what is typed via keyboard, inside the variable.

In your case:

printf("Digite o valor de a: \n");
scanf("%f", &a);

Above the function scanf will get the value typed by the user and put in the address of the variable a. The scanf function expects to receive this memory address of the variable and so you need to put the & in front of getting her &to (this is called passing parameters by reference and you will study when you see the functions subjects)

Already the function printf waiting that you put this &. The printf function only needs the variable name to get the content inside it and display this content on the screen, provided you specify its type (%d, %f , %c, etc).

Try, for example, putting %p instead of %f and see that the result shown on the screen will be the value of the memory address that the variables Delt, X1 and x2 occupy.

Note: %p indicates that you want the output formatted as a memory address

Endereço das variáveis

Observe the code with %p:

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

int main() {

    setlocale(LC_ALL, "Portuguese");

    float a, b, c, delt, x1, x2;


    printf("Digite o valor de a: \n");
    scanf("%f", &a);

    printf("Digite o valor de b: \n");
    scanf("%f", &b);

    printf("Digite o valor de c: \n");
    scanf("%f", &c);

    delt = b*b -4*a*c;
    printf("Delta = %p \n", &delt);

    if (delt >= 0) {

        x1 = (-b + sqrt(delt))/(2*a);
        x2 = (-b - sqrt(delt))/(2*a);
        printf("x1 = %p \n", &x1);
        printf("x2 = %p \n", &x2);
    }

        else {
        printf("A equação não tem raizes. \n");
        }

return 0;
} /* fim main*/

Note that now appears the memory address of the variables. It is appearing in your code 0.00000 because you have formatted a %f float in the output of a memory address that should be formatted as %p.

See now below your corrected code and the results. Remembering that %.0f indicates that we do not have decimals after the comma of a real number(float) If it were %.2f would indicate that you want two decimal places after the comma. and so on...

Delta e Raizes

#include <stdio.h>
#include <locale.h>
#include <math.h>
int main() {

setlocale(LC_ALL, "Portuguese");

float a, b, c, delt, x1, x2;


printf("Digite o valor de a: \n");
scanf("%f", &a);

printf("Digite o valor de b: \n");
scanf("%f", &b);

printf("Digite o valor de c: \n");
scanf("%f", &c);

delt = b*b -4*a*c;
printf("Delta = %.0f \n", delt);

if (delt >= 0) {

    x1 = (-b + sqrt(delt))/(2*a);
    x2 = (-b - sqrt(delt))/(2*a);
    printf("x1 = %.0f \n", x1);
    printf("x2 = %.0f \n", x2);
}

    else {
    printf("A equação não tem raizes. \n");
    }
return 0; 
}

Ready! Now it works! Study the concepts of the C language as it will help you a lot! Write down your questions and do not hesitate to ask! Hug!

Remember: Herr is Ubro! :)

Browser other questions tagged

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