# Recursive Exercise C #

4

I'm stuck with a recursive exercise in C #, then I leave the slogan:

-Make a tree as detailed below from the entry of a value of a variable.

Ex: Entry 5

`````` - - - - 1

- - - 2 2 2

- - 3 3 3 3 3

- 4 4 4 4 4 4 4

5 5 5 5 5 5 5 5 5
``````

(I do not know if it looks good, but the idea is to be in the form of triangular pine)

If someone could give me a hand to solve it, I would appreciate a lot, I think there is something in the statement that I am not able to reason.

The code I could reach is the following:

``````class Program
{
public class Arbol
{
void hacerArbol(int x)
{
if (x > 0)
{
hacerArbol(x - 1);

if (x < 5)
{
Console.Write("_" + " ");
}
else
{
Console.Write(x);
Console.WriteLine();
}
//hacerArbol(x - 1);
}
}

static void Main(string[] args)
{
Arbol nuevo = new Arbol();
nuevo.hacerArbol(5);
}
}
}
``````

I can not manage to pose the figure that is asked, I think there is a part that I can not imagine how to do.

asked by user3615 27.06.2016 в 16:12
source

3

I do not know if the order of the figure matters, I mean, whether the pine should be centered or aligned to the left or something like that.

But this code has worked for me to solve a pine tree aligned to the left:)

Its operation is simple.

``````public static void DrawTree(int max, int color = 0)
{
int n = color;

for (int i = 0; i < n; i++)
Console.Write(n.ToString() + " ");
Console.WriteLine();

if (color == max) return;

DrawTree(max, n + 1);
}
``````

First two parameters, the first ( `max` ), the number of iterations that will form the pine and the last ( `color` ) which is a default parameter which is 0 (on the first attempt) , with these two, it is drawn along recursively calling.

``````public static void Main()
{
DrawTree(5);
}
``````

With the above code, the following output occurs:

``````1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
``````

If you need the tree to be drawn towards the center: Add the following line before the loop `for` :

`````` Console.Write(new string(' ', max - color));
``````

Leaving the code like this:

``````public static void DrawTree(int max, int color = 0)
{
int n = color;
Console.Write(new string(' ', max - color));
for (int i = 0; i < n; i++)
Console.Write(n.ToString() + " ");
Console.WriteLine();

if (color == max) return;

DrawTree(max, n + 1);
}
``````

Which produces the following output:

``````    1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
``````

source
3

This exercise of recursion seems to me quite bad ... it has the peculiarity of needing to know the height in advance to be able to write the scripts of each floor so the function

``````hacerArbol(int x)
``````

does not work; but it can be done differently:

``````string hacerRama(int x, int y)
{
// Al llegar a x = 1, finaliza la recursividad.
if (x == 1)
return string.Concat(Enumerable.Repeat("- ", y - 1)) + '1';

// La llamada recursiva se hace en hacerRama no en hacerArbol
return hacerRama(x - 1, y) + Environtment.NewLine + Environtment.NewLine +
// Cada coleccion de guiones decrece a medida que bajamos ramas
string.Concat(Enumerable.Repeat("- ", y - x)) +
// El tamaño de cada rama es una secuencia de numeros impares: 1, 3, 5, 7, 9...
string.Concat(Enumerable.Repeat(x.ToString() + ' ', (x * 2) - 1));
}

string hacerArbol(int x)
{
// hacerRama es recursiva
return hacerRama(x, x);
}
``````

The resulting string to call `hacerArbol(5)` would be exactly the one you put in the example.

The trick is to concatenate the strings in reverse by putting the first call to `hacerRama` at the end of the string.

PS: From two-digit numbers does not work.