En este video te enseñare como se programa recursivamente, te mostrare 3 ejemplos con los cuales comprenderás esta forma de programar rápidamente.
Código fuente
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Recursividad
{
class Program
{
static void Main(string[] args)
{
//ejemplo 1: recorrer un arreglo
int[] arregloDeEnteros = new int[] { 2,1,1,1,1,1,2};
Recorrer(arregloDeEnteros);
//ejemplo 2: sumar elementos de un arreglo
int total = Sumar(arregloDeEnteros);
//ejemplo 3: pintar un arbolito
Bitmap bmp = new Bitmap(100,100);
Pintar(bmp,bmp.Width/2,20);
string path = @"C:\Users\Bioxor\source\repos\Recursividad\Recursividad\arbolito.jpg";
bmp.Save(path,ImageFormat.Bmp);
}
static void Pintar(Bitmap bmp,int x, int n,int y=0)
{
if (y < n)
{
bmp.SetPixel(x, y, Color.Red);
Pintar(bmp,x+1,n,y+1);
Pintar(bmp,x-1,n,y+1);
}
}
static int Sumar(int []arreglo,int posicion=0)
{
if (posicion == arreglo.Length)
return 0;
return arreglo[posicion] + Sumar(arreglo, posicion+1);
}
static void Recorrer(int [] arreglo,int posicion=0)
{
if (posicion == arreglo.Length)
return;
Console.WriteLine(arreglo[posicion]);
Recorrer(arreglo,posicion+1);
}
}
}
