vendredi 29 mai 2015

utopian tree using c

i am making a utopian tree in which if the tree height is 1 at first and every first year it doubles and second it increses by +1; so please suggest me right ans with cases for example There are 3 test cases.

In the first case (N=0), the initial height (1) of the tree remains unchanged.

In the second case (when N = 1, i.e. after the 1st cycle), the tree doubles its height as it's planted at the onset of spring.

In the third case (N=4), the tree first doubles its height (2), then grows a meter (3), then doubles again (6), before growing another meter; at the end of the 4th cycle, its height is 7 meters. and the sample code is

#include<stdio.h>

int main()
{
    int years,m,n,p,q,j;
    int height=1;
    int cases;
    int array[m];

    scanf("%d",&cases);

    for(m=0;m<cases;m++) 
    { 

     scanf("%d",&years[m]);
    }  

    j=0;

    for(n=0;n<=years[j];n++)
    {

       j<cases;

       j++;
       p=n;
       q=p%2;
       if(q==0)
          height=height*2;
       else
          height = height+1;
    }

  printf("%d",height);

 return 0;
}

I tried every possible way to do but I did not get the right answer.

Aucun commentaire:

Enregistrer un commentaire