N1K003

Untitled

Apr 23rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define ARRAY_COUNT ((int)10)
  6.  
  7. void fill_array(int *array);
  8. void print_array(int *array);
  9. float find_mean(int *array);
  10.  
  11. int main(void)
  12. {
  13.     srand(time(NULL));
  14.     int array[ARRAY_COUNT];
  15.  
  16.     fill_array(array);
  17.     print_array(array);
  18.     printf("mean: %0.2f\n", find_mean(array));
  19.  
  20.     system("pause");
  21.     return 0;
  22. }
  23.  
  24. void fill_array(int *array)
  25. {
  26.     if (array == NULL)
  27.     {
  28.         printf("Array is NULL pointer\n");
  29.         return;
  30.     }
  31.  
  32.     for (int i = 0; i < ARRAY_COUNT; i++)
  33.         array[i] = rand() % 100 + 1;
  34. }
  35.  
  36. void print_array(int *array)
  37. {
  38.     printf("=====\tARRAY\t=====\n");
  39.     if (array == NULL)
  40.     {
  41.         printf("Array is NULL pointer\n");
  42.         return;
  43.     }
  44.  
  45.     for (int i = 0; i < ARRAY_COUNT; i++)
  46.         printf("ARR[%2d] = %3d\n", i+1, array[i]);
  47.     printf("=====================\n");
  48. }
  49.  
  50. float find_mean(int *array)
  51. {
  52.     if (array == NULL)
  53.     {
  54.         printf("Array is NULL pointer\n");
  55.         return -1;
  56.     }
  57.  
  58.     float mean = .0;
  59.     int count = 0;
  60.  
  61.     for (int i = 0; i < ARRAY_COUNT; i++)
  62.     {
  63.         if (array[i] % 2 == 0) {
  64.             mean += array[i];
  65.             count++;
  66.         }
  67.     }
  68.  
  69.     return mean/count;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment