Ver Mensaje Individual
  #47 (permalink)  
Antiguo 29/11/2014, 15:49
Avatar de kspr
kspr
 
Fecha de Ingreso: agosto-2011
Ubicación: Ecuador
Mensajes: 43
Antigüedad: 12 años, 7 meses
Puntos: 7
Respuesta: Petando la pila. Problemas y retos usando recursividad.

El primer ejercicio lo intente simulando un pase por referencia,

Código C:
Ver original
  1. #include <stdio.h>
  2.  
  3. /* más codigo si hace falta */
  4.  
  5. void contarNegativos(int arr[], int n, int *cantidad)
  6. {
  7.     if(n > 0)
  8.     {
  9.         if(arr[n-1] < 0)
  10.         *cantidad = *cantidad + 1;
  11.  
  12.         contarNegativos(arr, n-1, cantidad);
  13.     }
  14. }
  15.  
  16. int main (void)
  17. {
  18.     int arr[] = {-1, -4, -3, 2, 1, 8, 0, 1};
  19.     int n = sizeof arr / sizeof *arr;
  20.     int cantidad = 0;
  21.  
  22.     /* calcular cantidad de negativos */
  23.     contarNegativos(arr, n, &cantidad);
  24.  
  25.     /* mostrar cantidad de negativos */
  26.     printf("\ntotal: %d\n", cantidad);
  27.  
  28.     return 0;
  29. }


solucion 2: http://ideone.com/MZsKX1

Última edición por kspr; 29/11/2014 a las 23:36