4
Good afternoon, I have a problem I need to do the following:
Create a program, in C language, that calculates the size of the tree of life, after a certain number of growth cycles, taking into account that the tree begins with a meter in size.
A tree, 1 meter in size, after 1 cycle, is 2 meters.
A tree, 1 meter in size, after 2 cycles, is 3 meters.
A tree, 1 meter in size, after 3 cycles, is 6 meters.
A tree, 1 meter in size, after 4 cycles, is 7 meters.
A tree, 1 meter in size, after 5 cycles, is 14 meters.
A tree, 1 meter in size, after 6 cycles, is 15 meters.
A tree, 1 meter in size, after 7 cycles, is 30 meters.
so far I’ve done the following
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void funcao_01() // sub programa
{
int ciclos;
printf(" \n\n -- ARVORE DA VIDA -- \n\n");// cabecalho
printf(" Digite o valor de ciclos desejados: ");
scanf("%d",&ciclos); // le valor digitado pelo usuario
float resultado;
if (ciclos % 2 == 1)
{
resultado = (ciclos * ciclos);
--resultado;
}
else
{
if(ciclos % 2 == 0)
{
resultado = ciclos * 2;
}
}
printf("\n\n Voce escolheu %d, ciclos\n\n Sendo assim voce tem %f metros.\n\n\n\n",ciclos, resultado);
}
int main() // programa principal
{
funcao_01(); // chama funcao
return 0;
}
because the relationship I had created was as follows:
"When the cycle is odd it doubles.. when it is even it adds +1"
in the beginning it worked out but then when it starts with larger numbers it doesn’t have more logic that, then I would like to "standardize" say so, but my question is, which is the easiest method to reach the resolution of this problem?
to '1' and '2' for sure! already to 3 in front of everything different, and another has to change the result value from FLOAT to INT, but anyway, already in 3 is in 8 Mts, 4 is in 5 Mts, 7 is in 128Mts
– AGenaro
True, the
if
have to check the indexi
and not the cycles. I have already arranged, is that I made the code inSwift
and confused when posting here– iTSangar
Well, there is another however, besides giving an error 'C99' in the line of the for
– AGenaro
@Accept this answer here, which is correct. The C99 error you received is because you are using a compiler configured for an old C standard, called C89, which does not accept declaring variables directly in for (
for (int i = 1 ...
). Your question does not require the answer to be C89, so this answer here is correct.– Pablo Almeida