Ver Mensaje Individual
  #3 (permalink)  
Antiguo 14/11/2014, 13:39
kutcher
 
Fecha de Ingreso: noviembre-2014
Mensajes: 36
Antigüedad: 9 años, 5 meses
Puntos: 13
Respuesta: Petando la pila. Problemas y retos usando recursividad.

Excelente iniciativa Pantaláimon me gusta mucho este tipo de retos

Solución al primer reto:

Código C++:
Ver original
  1. #include <stdio.h>
  2.  
  3. int contarNegativos(int arr[], int n)
  4. {
  5.     if (n == 0) return(0);
  6.     return (arr[n - 1] < 0 ? 1 : 0) + contarNegativos(arr, n - 1);
  7. }
  8.  
  9. int main (void)
  10. {
  11.     int arr[] = {1, 4, -3, 2, -1, -8, 0, 1};
  12.     int n = sizeof arr / sizeof *arr;
  13.  
  14.     int cantidad = contarNegativos(arr, n);
  15.     printf("%d\n", cantidad);
  16.  
  17.     return(0);
  18. }

Segundo:

Código C++:
Ver original
  1. #include <stdio.h>
  2.  
  3. char diag(char *str, int n)
  4. {
  5.     if (str[n] == '\0') return(0);
  6.     printf( "%*c\n", n + 1, str[n]);
  7.     return diag(str, n + 1);
  8. }
  9.  
  10. int main (void)
  11. {
  12.     char cadena [] = "abcde";
  13.     diag (cadena, 0);
  14.  
  15.     return(0);
  16. }

Saludos y a la espera de mas retos

Última edición por kutcher; 14/11/2014 a las 15:07