Advertisement
STANAANDREY

filter

Mar 2nd, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define NMAX 100
  4.  
  5. void delete(int arr[], int *n, int pos) {
  6.   for (int i = pos; i < *n - 1; i++) {
  7.     arr[i] = arr[i + 1];
  8.   }
  9.   (*n)--;
  10. }
  11.  
  12. void filter(int arr[], int *n, bool (*cond)(int)) {
  13.   for (int i = 0; i < *n; i++) {
  14.     if (!cond(arr[i])) {
  15.       delete(arr, n, i--);
  16.     }
  17.   }
  18. }
  19.  
  20. bool cond(int x) {
  21.   return x < 0;
  22. }
  23.  
  24. int main(void) {
  25.   int n;
  26.   scanf("%d", &n);
  27.   static int arr[NMAX];
  28.   for (int i = 0; i < n; i++) {
  29.     scanf("%d", arr + i);
  30.   }
  31.  
  32.   filter(arr, &n, cond);
  33.  
  34.   for (int i = 0; i < n; i++) {
  35.     printf("%d ", arr[i]);
  36.   }
  37.   puts("");
  38.   return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement