Miscalculation

Asked

Viewed 64 times

-2

#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 displayed zero value.

1 answer

3


The main problem is that the formatting pattern used in scanf() is wrong, is using to enter a whole and wants a kind of floating point that is the %f. That’s how it 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));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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