Advertisement
LilChicha174

Untitled

Apr 21st, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int count_if(void *base, size_t num, size_t size, int(*pred)(const void *)) {
  6.     int count = 0;
  7.     void *elem = base;
  8.     for (int i = 1; i < num + 1; i++) {
  9.         if (pred(elem) == 1)
  10.             count++;
  11.         elem = base + i * size;
  12.     }
  13.     return count;
  14. }
  15.  
  16. int pred_int(const void *base){
  17.     int elem = *(int*)base;
  18.     if(abs(elem)%2==0)
  19.         return 1;
  20.     return 0;
  21. }
  22.  
  23. int pred_double(const void *base) {
  24.     double elem = *(double *) base;
  25.     if (((int)abs(elem) % 2 == 0) && (((fabs(elem) - abs((int) elem))) < 0.0000001)) {
  26.         return 1;
  27.     }
  28.     return 0;
  29. }
  30.  
  31. int main(){
  32.     int n = 20;
  33.     int size = 0;
  34.     scanf("%d", &size);
  35.     if (size==4){
  36.         int input_array[n];
  37.         for(int i = 0; i<n; i++)
  38.             scanf("%d", &input_array[i]);
  39.         int p = count_if(input_array, n, sizeof(int), pred_int);
  40.         printf("%d", p);
  41.     }
  42.     if (size==8){
  43.         double input_array[n];
  44.         for(int i = 0; i<n; i++)
  45.             scanf("%lf", &input_array[i]);
  46.         int p = count_if(input_array, n, sizeof(double), pred_double);
  47.         printf("%d", p);
  48.     }
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement