Advertisement
Krudener

Untitled

Sep 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define ERROR -1
  6. #define OK 0
  7. #define CORRECT_SCAN 1
  8. #define QUANTITY_OF_MU2 3
  9. #define MIN_SIZE_OF_N 1
  10. #define MIN_SIZE_OF_P 0
  11.  
  12. void task_one(double *array, double **array_end, double mu)
  13. {
  14.     double *ptr = array;
  15.     int flag = 0;
  16.     double temp;
  17.     while (ptr < *array_end)
  18.     {
  19.         if (fabs(*ptr) < fabs(mu))
  20.         {
  21.             flag = 1;
  22.             printf("task done %lf %lf \n ", *ptr, mu);
  23.             double *swap_ptr = ptr;
  24.             while (swap_ptr < *array_end)
  25.             {
  26.                 temp = *(swap_ptr + 1);
  27.                 *(swap_ptr + 1) = *swap_ptr;
  28.                 *swap_ptr = temp;
  29.                 swap_ptr++;
  30.             }
  31.             --(*array_end);
  32.         }
  33.         if (flag == 0)
  34.         {
  35.             ptr++;
  36.         }
  37.         else
  38.         { flag = 0; }
  39.     }
  40. }
  41.  
  42.  
  43. double average_cubic_modules(double *array, double *array_end)
  44. {
  45.     double mu = 0;
  46.     int length = array_end - array;
  47.     for (double *ptr = array; ptr < array_end - 1; ptr++)
  48.     {
  49.         mu += pow(fabs(*ptr), 3) / length;
  50.         printf("mu = %lf \n", mu);
  51.     }
  52.     mu = cbrt(mu);
  53.     return mu;
  54. }
  55.  
  56. double min_element_of_array(double *array, double *array_end)
  57. {
  58.     double mu_2 = *array;
  59.     for (double *ptr = array; ptr < array_end; ptr++)
  60.     {
  61.         if (*ptr < mu_2)
  62.         {
  63.             mu_2 = *ptr;
  64.         }
  65.     }
  66.     return mu_2;
  67. }
  68.  
  69. int input_array(double *array, double *array_end)
  70. {
  71.     printf("Please input array elements:");
  72.     if (array)
  73.     {
  74.         for (double *ptr = array; ptr < array_end; ptr++)
  75.         {
  76.             if (scanf("%lf", ptr) != CORRECT_SCAN)
  77.             {
  78.                 return ERROR;
  79.             }
  80.         }
  81.     }
  82.     else
  83.     {
  84.         return ERROR;
  85.     }
  86.     return OK;
  87. }
  88.  
  89. int main()
  90. {
  91.     printf("Please input n:");
  92.     int n;
  93.     if ((scanf("%d", &n) != CORRECT_SCAN) || ((n < MIN_SIZE_OF_N)))
  94.     {
  95.         return ERROR;
  96.     }
  97.  
  98.     double *array = malloc(n * sizeof(double));
  99.     if (array == NULL)
  100.     {
  101.         free(array);
  102.         return ERROR;
  103.     }
  104.     double *array_end = array + n;
  105.     if (input_array(array, array_end) == ERROR)
  106.     {
  107.         return ERROR;
  108.     }
  109.  
  110.     double mu = average_cubic_modules(array, array_end);
  111.     printf("mu = %lf \n", mu);
  112.     task_one(array, &array_end, mu);
  113.     printf("\n");
  114.     for (double *ptr = array; ptr < array_end; ptr++)
  115.     {
  116.         printf("%lf ", *ptr);
  117.     }
  118.  
  119.     double mu_2 = min_element_of_array(array, array_end);
  120.     printf("\n");
  121.     printf("mu2 = %lf \n", mu_2);
  122.  
  123.     printf("Please input p:");
  124.     int p;
  125.     if ((scanf("%d", &p) != CORRECT_SCAN) || (p < MIN_SIZE_OF_P) || ((array_end - array) < (p + 1)))
  126.     {
  127.         return ERROR;
  128.     }
  129.  
  130.     array = realloc(array, (n + QUANTITY_OF_MU2) * sizeof(double));
  131.     free(array);
  132.     return OK;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement