Problems with calculating "TREE OF LIFE"

Asked

Viewed 756 times

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?

2 answers

7

Come on:

Create a program, in C language, that calculates the size of the life, after a certain number of growth cycles

As the statement said we will create a program that calculates the size of the tree of life, so we already have a name for our program:

void calculaArvoreDaVida() {
  //
}


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.

"When the cycle is odd it doubles.. when it is even it adds +1"

The logic is correct only the algorithm that does not:

// usamos um loop for para percorrer a quantidade de ciclos
for(int i = 1; i <= ciclos; i++) {
    
    // verificamos se i é impar ou par 
    if (i % 2 == 1) {
        // se for impar multiplicamos o resultado por 2
        resultado = resultado * 2;
    } else {
        // se for par somamos + 1
        resultado = resultado + 1;
    }
}


Finally:

taking into account that the tree begins at one meter in size.

Do not forget to initialize the variable resultado:

int resultado = 1;

Ps.: Check the code as I have not tested on C.

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

void calculaArvoreDaVida() // 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

    int resultado = 1;

    for(int i = 1; i <= ciclos; i++) {        
        if (i % 2 == 1) {
            resultado = resultado * 2;
        } else {
            resultado = resultado + 1;
        }
    }

    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
{
    calculaArvoreDaVida(); // chama funcao
    return 0;
}
  • 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

  • True, the if have to check the index i and not the cycles. I have already arranged, is that I made the code in Swift and confused when posting here

  • Well, there is another however, besides giving an error 'C99' in the line of the for

  • @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.

0

RESOLVED:

'cause I’ve solved the problem I’ll put the code here for anyone who wants to see the resolution.

#include <stdio.h>
void calculaArvoreDaVida() // sub programa
{
    int ciclos;
    int resultado = 1;
    int x;
    do
    {
        // cabecalho
        printf(" \n\n             --   ARVORE DA VIDA   --  \n\n");
        printf(" Digite 0 (zero) para sair do programa\n\n");
        printf("  Digite o valor de ciclos desejados: ");
        scanf("%d",&ciclos); // le valor digitado pelo usuario
        if (ciclos == 0) // se o usuario digitar zero em tela imprime mensagem
        {
            printf("\n\n         BYE BYE !  \n\n"); // mensagem exibida caso valor digitado for zero
            return;
        }
        for(x = 1; x <= ciclos; x++ ) // teste e implementacao
        {
            if (x % 2 == 1)
            {
                resultado = resultado * 2;
            }
            else
            {
                resultado = resultado + 1;
            }
        }
        //mostra mensagem na tela caso sucesso
        printf("\n\n  Voce escolheu %d, ciclos\n\n  Sendo assim voce tem %d metros.\n\n\n\n",ciclos, resultado);
        // volta variavel resultado para 1 para refazer o ciclo sem nenhuma variavel atribuida.
        // pois se nao tiver ele vai pegar valores anteriores e manipular apartir daqueles
        // sendo assim nao da certo.
        resultado = 1;
    }
    while(ciclos != 0);
}
// PROGRAMA PRINCIPAL
int main() 
{
    calculaArvoreDaVida(); // chama funcao
    return 0;
} 

Browser other questions tagged

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