Advertisement
informaticage

C excercise arrays template

Jul 27th, 2021
1,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Aggiunge alla fine
  5. void append();
  6.  
  7. // Aggiunge all'inizio
  8. void prepend();
  9.  
  10. // Aggiunge all'indirizzo
  11. void insert_at();
  12.  
  13. // Adds element without ruining sorting property
  14. // Only works for previusly sorted arrays
  15. // Example:
  16. // -> 15
  17. // 1 9 10 (15) 29 32
  18. void insert_sorted();
  19.  
  20. int main() {
  21.   int N = 10;
  22.   int *v = (int *)malloc(sizeof(int) * N);
  23.  
  24.   for (int i = 0; i < N; i++) {
  25.     v[i] = 2 * i;
  26.   }
  27.  
  28.   for (int i = 0; i < N; i++) {
  29.     printf("%d ", v[i]);
  30.   }
  31.  
  32.   // ...
  33.   // Insert solution here
  34.   // ...
  35.   char choice;
  36.   printf("Insert choice:\nA to prepend.\nB to append.\nC to add at specific poition.\n\n");
  37.   do {
  38.     scanf("%c", &choice);
  39.     switch (choice)
  40.     {
  41.     case 'a':
  42.     case 'A':
  43.       // tua scanf (occhio ai buffer)
  44.       // Tua chiamata a funzione
  45.       printf("Insert item to be prepended: ");
  46.       break;
  47.  
  48.     case 'b':
  49.     case 'B':
  50.       // tua scanf (occhio ai buffer)
  51.       // Tua chiamata a funzione
  52.       printf("Insert item to appended: ");
  53.       break;
  54.  
  55.     case 'c':
  56.     case 'C':
  57.       // tua scanf (occhio ai buffer)
  58.       // Tua chiamata a funzione
  59.       printf("Insert item to be inserted and position: ");
  60.       break;
  61.  
  62.     default:
  63.       break;
  64.     }
  65.   } while(choice != '-');
  66.  
  67.   free(v);
  68.   return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement