Guest User

Untitled

a guest
Oct 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. #define N 8
  5.  
  6.  
  7. /** Recursive delete like a pro
  8. * where: 'ar' is the array, 'n' is the size, 'start' is 0 when
  9. * called first time, 'irem' is the index of element to remove.
  10. * @return the new size of the array (n-1)
  11. */
  12. int recdel (int *ar, int n, int start, int irem)
  13. {
  14. if (start == n-1)
  15. return start;
  16.  
  17. if (start >= (irem-1))
  18. ar[start] = ar[start+1];
  19.  
  20. return recdel (ar,n,start+1,irem);
  21. }
  22.  
  23.  
  24.  
  25.  
  26. void print_ar (int *a, int n)
  27. {
  28. for (int i=0;i<n;i++) printf ("%d ", a[i]);
  29. printf ("\n\n");
  30. }
  31.  
  32. int main ()
  33. {
  34. int *ar = (int *) malloc (sizeof(int) * N);
  35.  
  36. // Inizializzo con numeri a caso
  37. for (int i=0; i<N; i++) ar[i] = i+1;
  38.  
  39. print_ar (ar, N);
  40.  
  41. printf ("Elimino il 6' elemento\n");
  42.  
  43. // Elimino il 6 elemento e ricompatto
  44. int n = recdel (ar, N, 0, 6);
  45.  
  46. print_ar (ar, n);
  47. }
Add Comment
Please, Sign In to add comment