SHARE
TWEET

Untitled

a guest Sep 17th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define CORRECT_ENDING 0
  6. #define INVALID_POINTER -1
  7. #define WRONG_DATA -2
  8. #define OUT_OF_DATA -3
  9. #define EMPTY_ARRAY -4
  10. #define INVALID_POSITION -5
  11. #define INVALID_SIZE -6
  12. #define SCANNING_FAILURE -7
  13. #define EPS 0.001
  14.  
  15. typedef double* ptr_t;
  16.  
  17. void free_array(ptr_t *ast, ptr_t *afn);
  18. void output_array(ptr_t ast, ptr_t afn);
  19. int create_array(ptr_t *ast, ptr_t *afn, int n);
  20. int resize_array(ptr_t *ast, int newsize);
  21. int len(ptr_t ast, ptr_t afn);
  22. double find_min(ptr_t ast, ptr_t afn);
  23. double average_cubic(ptr_t ast, ptr_t afn);
  24. int delete_smaller(ptr_t *ast, ptr_t *afn, double num);
  25. int insert_on_pos(ptr_t *ast, ptr_t *afn, size_t pos, double num);
  26.  
  27. int create_array(ptr_t *ast, ptr_t *afn, int n)
  28. {
  29.     int rc;
  30.     *ast =  (ptr_t) malloc(sizeof(double) * n);
  31.  
  32.     if (!ast)
  33.     {
  34.         free(*ast);
  35.         return INVALID_POINTER;
  36.     }
  37.  
  38.     *afn = *ast + n;
  39.  
  40.  
  41.     for (ptr_t i = *ast; i < *afn; i++)
  42.     {
  43.         rc = scanf("%lf", i);
  44.         if (!rc)
  45.         {
  46.             for (ptr_t j = *ast; j < i; j++)
  47.             {
  48.                 j = NULL;
  49.                 free(j);
  50.             }
  51.             return WRONG_DATA;
  52.         }
  53.     }
  54.  
  55.     return CORRECT_ENDING;
  56. }
  57.  
  58. int resize_array(ptr_t *ast, int newsize)
  59. {
  60.     double * newp = (double *) realloc(*ast, newsize * sizeof(double));
  61.  
  62.     if (!*newp) return OUT_OF_DATA;
  63.  
  64.     *ast = newp;
  65.     newp = NULL;
  66.     free(newp);
  67.     return CORRECT_ENDING;
  68. }
  69.  
  70. void output_array(ptr_t ast, ptr_t afn)
  71. {
  72.     for (ptr_t i = ast; i < afn; i++)
  73.         printf("%lf ", *i);
  74. }
  75.  
  76. void free_array(ptr_t *ast, ptr_t *afn)
  77. {
  78.     if (ast != NULL && afn != NULL)
  79.     {
  80.         for (ptr_t i = *ast; i < *afn; i++)
  81.         {
  82.             i = NULL;
  83.             free(i);
  84.         }
  85.     }
  86.  
  87.     afn = NULL;
  88. }
  89.  
  90. int len(ptr_t ast, ptr_t afn)
  91. {
  92.     return (afn - ast);
  93. }
  94.  
  95. double find_min(ptr_t ast, ptr_t afn)
  96. {
  97.     double min = *ast;
  98.     for (ptr_t i = ast + 1; i < afn; i++)
  99.         if (*i < min) min = *i;
  100.     return min;
  101. }
  102.  
  103. double average_cubic(ptr_t ast, ptr_t afn)
  104. {
  105.     double result = 0;
  106.     for (ptr_t j = ast; j < afn; j++)
  107.         result += *j * *j * fabs(*j);
  108.  
  109.     result/= len(ast, afn);
  110.  
  111.     return cbrt(result);
  112. }
  113.  
  114. int delete_smaller(ptr_t *ast, ptr_t *afn, double num)
  115. {
  116.     for (ptr_t i = *ast; i < *afn; i++)
  117.     {
  118.         if (fabs(fabs(*i) - EPS) < num)
  119.             for (ptr_t j = i; j < *afn; j++)
  120.                 *j = *j + 1;
  121.         (*afn)--;
  122.     }
  123.     if (!len(*ast, *afn))
  124.     {
  125.         free_array(ast, afn);
  126.         return EMPTY_ARRAY;
  127.     }
  128.  
  129.     return CORRECT_ENDING;
  130. }
  131.  
  132. int insert_on_pos(ptr_t *ast, ptr_t *afn, size_t pos, double num)
  133. {
  134.     int rc;
  135.     if (pos > len(*ast, *afn) - 1 || pos < 0)
  136.         return INVALID_POSITION;
  137.  
  138.     rc = resize_array(afn, len(*ast, *afn) + 1);
  139.     if (!rc)
  140.         return OUT_OF_DATA;
  141.  
  142.     for (ptr_t j = *ast; j < *afn; j++)
  143.         *(j + 1) = *j;
  144.     **(ast + pos) = num;
  145.     return CORRECT_ENDING;
  146. }
  147.  
  148.  
  149. int error_msg(int code)
  150. {
  151.     switch (code)
  152.     {
  153.         case INVALID_POINTER:
  154.             puts("The pointer is invalid.");
  155.             break;
  156.         case WRONG_DATA:
  157.             puts("The raw data is invalid.");
  158.             break;
  159.         case OUT_OF_DATA:
  160.             puts("Allocation failed due to lack of memory.");
  161.             break;
  162.         case EMPTY_ARRAY:
  163.             puts("Array is fully deleted.");
  164.             break;
  165.         case INVALID_POSITION:
  166.             puts("Position is out of borders.");
  167.             break;
  168.         case INVALID_SIZE:
  169.             puts("Size of array is invalid.");
  170.             break;
  171.         case SCANNING_FAILURE:
  172.             puts("Scanning of number went wrong.");
  173.             break;
  174.     }
  175.  
  176.     return code;
  177. }
  178.  
  179.  
  180. int main(void)
  181. {
  182.    int code;
  183.    int size;
  184.    ptr_t ast;
  185.    ptr_t afn;
  186.    double m1, m2;
  187.    int pos;
  188.  
  189.    ast = NULL;
  190.    afn = NULL;
  191.  
  192.    printf("Enter length of array:\n");
  193.    code = scanf("%d", &size);
  194.    if (code != 1) return SCANNING_FAILURE;
  195.  
  196.    code = create_array(&ast, &afn, size);
  197.  
  198.    if (code != CORRECT_ENDING) return error_msg(code);
  199.  
  200.    m1 = average_cubic(ast, afn);
  201.  
  202.    code = delete_smaller(&ast, &afn, m1);
  203.    if (code != CORRECT_ENDING) return error_msg(code);
  204.  
  205.    code = resize_array(&afn, len(ast, afn));
  206.    if (code != CORRECT_ENDING) return error_msg(code);
  207.  
  208.    output_array(ast, afn);
  209.  
  210.    m2 = find_min(ast, afn);
  211.  
  212.    printf("Enter pos of insertion:\n");
  213.    code = scanf("%d", &pos);
  214.    if (code != 1) return SCANNING_FAILURE;
  215.  
  216.    code = insert_on_pos(&ast, &afn, pos, m2);
  217.    if (code != CORRECT_ENDING) return error_msg(code);
  218.  
  219.    output_array(ast, afn);
  220.  
  221.    return CORRECT_ENDING;
  222. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top