Advertisement
N1K003

Untitled

Apr 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /* defining array members count */
  6. #define ARRAY_COUNT ((int)10)
  7.  
  8. /* delcaring functions */
  9. void fill_array(int *array);
  10. void print_array(int *array);
  11. float find_mean(int *array);
  12.  
  13. /* entry point */
  14. int main(void)
  15. {
  16.     /* preparing randomizer */
  17.     srand(time(NULL));
  18.     /* declaring array */
  19.     int array[ARRAY_COUNT];
  20.    
  21.     /* working */
  22.     fill_array(array);
  23.     print_array(array);
  24.     printf("mean: %0.2f\n", find_mean(array));
  25.  
  26.     /* waiting for exit */
  27.     return system("pause");
  28. }
  29.  
  30. /* filling array with rundom numbers */
  31. void fill_array(int *array)
  32. {
  33.     /* we don't want to work with NULLs */
  34.     if (array == NULL)
  35.     {
  36.         printf("Array is NULL pointer\n");
  37.         return;
  38.     }
  39.  
  40.     /* generating rundoms and writing tham to array */
  41.     for (int i = 0; i < ARRAY_COUNT; i++)
  42.         array[i] = rand() % 100 + 1;
  43. }
  44.  
  45. /* printing array to stdout */
  46. void print_array(int *array)
  47. {
  48.     printf("=====\tARRAY\t=====\n");
  49.     /* we don't want to work with NULLs */
  50.     if (array == NULL)
  51.     {
  52.         printf("Array is NULL pointer\n");
  53.         return;
  54.     }
  55.  
  56.     for (int i = 0; i < ARRAY_COUNT; i++)
  57.         printf("ARR[%2d] = %3d\n", i+1, array[i]);
  58.     printf("=====================\n");
  59. }
  60.  
  61. /* finding mean */
  62. float find_mean(int *array)
  63. {
  64.     /* we don't want to work with NULLs */
  65.     if (array == NULL)
  66.     {
  67.         printf("Array is NULL pointer\n");
  68.         return -1;
  69.     }
  70.  
  71.     float mean = .0;
  72.     int count = 0;
  73.  
  74.     for (int i = 0; i < ARRAY_COUNT; i++)
  75.     {
  76.         if (array[i] % 2 == 0) {
  77.             mean += array[i];
  78.             count++;
  79.         }
  80.     }
  81.  
  82.     return mean/count;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement