C: Result of delta, x1 and x2 always giving 0.000000

0

I'm learning C in college, and I'm stuck with this algorithm. I can not find the error. It can tell 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"); 
        }
}
    
asked by anonymous 11.08.2017 / 17:41

3 answers

0

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

So 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, variables are allocated in memory and therefore have an address where they are used to access this content.

In basic language programming C, without using pointers, to access the contents of a variable simply write the name of this variable and compiler will be in charge of finding the address of it and bring or write something inside the variable.

In the case of the scanf function, it needs to know the address of the variable's memory region in order to be able to enter what is typed through the keyboard.

In your case:

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

Above the scanf function will get the value entered by the user and put in the address of the variable a. The scanf function expects to receive this memory address from the variable and so you need to put the & in front of it and & a (this is called parameter passing by reference and you will study when viewing the subjects functions)

The printf function does not wait for you to & . The printf function only needs the name of the variable to get the content that is inside it and display this content on the screen, as long as you specify its type (% d,% f,% c, etc.).

Try, for example, to put % p instead of% f and see that the result displayed on the screen will be the value of the memory address that the variables oft, x1 and x2 occupy. p>

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

Notethecodewith%p:

#include<stdio.h>#include<locale.h>#include<math.h>intmain(){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 the variable memory address now appears. It is showing in your code 0.00000 because you have formatted a float% f in the output of a memory address that should be formatted as% p.

See below for your corrected code and results. Remember that %. 0f indicates that it is not to have decimals after the comma of a real number (float) If it were %. 2f it would indicate that you want two decimal places after the comma. and so it goes ...

#include<stdio.h>#include<locale.h>#include<math.h>intmain(){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 C concepts because it will help you a lot! Write down your questions and do not hesitate to ask! Hugs!

Remember: H err U hand! :)

    
11.08.2017 / 18:34
1

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

    
11.08.2017 / 17:49
1

O & before a variable indicates that you are looking for the address of this and not the content, so that at the time you receive a data you use the & variable because you are saving that data in the address of the variable, but when you print you should only call the variable because then you want the content.

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

The same applies to other impressions

If you want to better understand the content, it will be useful to study pointers.

    
11.08.2017 / 17:57