Wrong calculation

-1
#include <stdio.h>
#include <stdlib.h>

float dobro_do_maior(float x, float y)
{
    float dobro;
    if(x > y)
    {
        dobro = x * 2;
    }else{
        dobro = y * 2;
    }
    return dobro;

}

void main()
{
    float a, b, resultado;
    printf("Digite dois numeros: ");
    scanf("%d", &a);
    scanf("%d", &b);
    resultado = dobro_do_maior(a, b);
    printf("o dobro é %f\n", resultado);

}

The problem is that this algorithm is set to zero.

    
asked by anonymous 25.11.2018 / 20:39

1 answer

3

The main problem is that the formatting pattern used in scanf() is wrong, you are using to enter an integer and either a floating-point type that is %f . This works:

#include <stdio.h>

float dobro_do_maior(float x, float y) {
    return x > y ? x * 2 : y * 2;
}

int main() {
    float a, b;
    printf("Digite dois numeros: ");
    scanf("%f", &a);
    scanf("%f", &b);
    printf("o dobro é %f\n", dobro_do_maior(a, b));
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
25.11.2018 / 20:44