Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #define N 8
- /** Recursive delete like a pro
- * where: 'ar' is the array, 'n' is the size, 'start' is 0 when
- * called first time, 'irem' is the index of element to remove.
- * @return the new size of the array (n-1)
- */
- int recdel (int *ar, int n, int start, int irem)
- {
- if (start == n-1)
- return start;
- if (start >= (irem-1))
- ar[start] = ar[start+1];
- return recdel (ar,n,start+1,irem);
- }
- void print_ar (int *a, int n)
- {
- for (int i=0;i<n;i++) printf ("%d ", a[i]);
- printf ("\n\n");
- }
- int main ()
- {
- int *ar = (int *) malloc (sizeof(int) * N);
- // Inizializzo con numeri a caso
- for (int i=0; i<N; i++) ar[i] = i+1;
- print_ar (ar, N);
- printf ("Elimino il 6' elemento\n");
- // Elimino il 6 elemento e ricompatto
- int n = recdel (ar, N, 0, 6);
- print_ar (ar, n);
- }
Add Comment
Please, Sign In to add comment