How to calculate months of a year from a decimal number?

Asked

Viewed 1,532 times

5

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

int main()
{
    printf("\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    float tempo(int horas);
    float converter(float x);
    int num_horas;
    float dias,anos;
    printf("Quantas horas vai dedicar por dia para ser genio?");
    scanf("%d",&num_horas);
    dias=tempo(num_horas);
    anos=converter(dias);
    printf("Voce vai demorar %.1f dias ou seja aproximadamente %.2f anos para ficar génio",dias,anos);
    return 0;
}
//funcao para converter dias em anos
float converter(float x) {
if(x>366) {
    int um_ano=366;
    float troca=x/um_ano;
    return troca;
}
}
//funcao que converte as horas dedicadas em dias;
float tempo(int horas) {
    float dias;
    dias=10000/horas;
    return dias;

}

The code works, but I wanted to improve on that part when it does printf and says how many years it takes to be a genius. He makes a float(on purpose), but I wanted to convert the decimal part into months. For example 4.22 years, I wanted it to stay 4 years and I don’t know how many months(.22).

I accept constructive criticism.

1 answer

5


I did what I understood. Obviously it is not very precise, it can give strange results in some cases, but only for an exercise is good.

There are several things that could be improved in a specific context. As it is exercise, having the functions may make sense, but to do something so simple it will only be used once, it makes no sense to have them.

Note the small details that I rewrote to get more organized, see how it gets easier to read and leaner. It could be even more. There is a variable left over. Note that I gave better names for the functions, so you don’t need comments. I got other small defects.

#include <stdio.h>

float converterDiasEmAnos(float dias) {
    return dias / 365;
}
float converterHorasEmDias(int horas) {
    return 10000 / horas;
}
int obterMeses(float anos) {
    return (anos - (int)anos) / (1.0f / 12.0f) + 1;
}
int main() {
    printf("\tPrograma para saber quanto demora a tornar-se genio!\n\n");
    int num_horas;
    printf("Quantas horas vai dedicar por dia para ser genio? ");
    scanf("%d", &num_horas);
    float dias = converterHorasEmDias(num_horas);
    float anos = converterDiasEmAnos(dias);
    int meses = obterMeses(anos);
    printf("\nVoce vai demorar %.1f dias, ou seja, aproximadamente %d anos e %d meses para ficar genio", dias, (int)anos, meses);
}

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

  • Can you explain this part? (1.0f / 12.0f) + 1; I did not understand very well. Otherwise it is ok.

  • This part is just math, there’s nothing programming, I did what you asked, turn decimal into months, just add 1 to round up and not create an expectation that time would be lower than it really is.

Browser other questions tagged

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