Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define CORRECT_ENDING 0
- #define INVALID_POINTER -1
- #define WRONG_DATA -2
- #define OUT_OF_DATA -3
- #define EMPTY_ARRAY -4
- #define INVALID_POSITION -5
- #define INVALID_SIZE -6
- #define SCANNING_FAILURE -7
- #define EPS 0.001
- typedef double* ptr_t;
- void free_array(ptr_t *ast, ptr_t *afn);
- void output_array(ptr_t ast, ptr_t afn);
- int create_array(ptr_t *ast, ptr_t *afn, int n);
- int resize_array(ptr_t *ast, int newsize);
- int len(ptr_t ast, ptr_t afn);
- double find_min(ptr_t ast, ptr_t afn);
- double average_cubic(ptr_t ast, ptr_t afn);
- int delete_smaller(ptr_t *ast, ptr_t *afn, double num);
- int insert_on_pos(ptr_t *ast, ptr_t *afn, size_t pos, double num);
- int create_array(ptr_t *ast, ptr_t *afn, int n)
- {
- int rc;
- *ast = (ptr_t) malloc(sizeof(double) * n);
- if (!ast)
- {
- free(*ast);
- return INVALID_POINTER;
- }
- *afn = *ast + n;
- for (ptr_t i = *ast; i < *afn; i++)
- {
- rc = scanf("%lf", i);
- if (!rc)
- {
- for (ptr_t j = *ast; j < i; j++)
- {
- j = NULL;
- free(j);
- }
- return WRONG_DATA;
- }
- }
- return CORRECT_ENDING;
- }
- int resize_array(ptr_t *ast, int newsize)
- {
- double * newp = (double *) realloc(*ast, newsize * sizeof(double));
- if (!*newp) return OUT_OF_DATA;
- *ast = newp;
- newp = NULL;
- free(newp);
- return CORRECT_ENDING;
- }
- void output_array(ptr_t ast, ptr_t afn)
- {
- for (ptr_t i = ast; i < afn; i++)
- printf("%lf ", *i);
- }
- void free_array(ptr_t *ast, ptr_t *afn)
- {
- if (ast != NULL && afn != NULL)
- {
- for (ptr_t i = *ast; i < *afn; i++)
- {
- i = NULL;
- free(i);
- }
- }
- afn = NULL;
- }
- int len(ptr_t ast, ptr_t afn)
- {
- return (afn - ast);
- }
- double find_min(ptr_t ast, ptr_t afn)
- {
- double min = *ast;
- for (ptr_t i = ast + 1; i < afn; i++)
- if (*i < min) min = *i;
- return min;
- }
- double average_cubic(ptr_t ast, ptr_t afn)
- {
- double result = 0;
- for (ptr_t j = ast; j < afn; j++)
- result += *j * *j * fabs(*j);
- result/= len(ast, afn);
- return cbrt(result);
- }
- int delete_smaller(ptr_t *ast, ptr_t *afn, double num)
- {
- for (ptr_t i = *ast; i < *afn; i++)
- {
- if (fabs(fabs(*i) - EPS) < num)
- for (ptr_t j = i; j < *afn; j++)
- *j = *j + 1;
- (*afn)--;
- }
- if (!len(*ast, *afn))
- {
- free_array(ast, afn);
- return EMPTY_ARRAY;
- }
- return CORRECT_ENDING;
- }
- int insert_on_pos(ptr_t *ast, ptr_t *afn, size_t pos, double num)
- {
- int rc;
- if (pos > len(*ast, *afn) - 1 || pos < 0)
- return INVALID_POSITION;
- rc = resize_array(afn, len(*ast, *afn) + 1);
- if (!rc)
- return OUT_OF_DATA;
- for (ptr_t j = *ast; j < *afn; j++)
- *(j + 1) = *j;
- **(ast + pos) = num;
- return CORRECT_ENDING;
- }
- int error_msg(int code)
- {
- switch (code)
- {
- case INVALID_POINTER:
- puts("The pointer is invalid.");
- break;
- case WRONG_DATA:
- puts("The raw data is invalid.");
- break;
- case OUT_OF_DATA:
- puts("Allocation failed due to lack of memory.");
- break;
- case EMPTY_ARRAY:
- puts("Array is fully deleted.");
- break;
- case INVALID_POSITION:
- puts("Position is out of borders.");
- break;
- case INVALID_SIZE:
- puts("Size of array is invalid.");
- break;
- case SCANNING_FAILURE:
- puts("Scanning of number went wrong.");
- break;
- }
- return code;
- }
- int main(void)
- {
- int code;
- int size;
- ptr_t ast;
- ptr_t afn;
- double m1, m2;
- int pos;
- ast = NULL;
- afn = NULL;
- printf("Enter length of array:\n");
- code = scanf("%d", &size);
- if (code != 1) return SCANNING_FAILURE;
- code = create_array(&ast, &afn, size);
- if (code != CORRECT_ENDING) return error_msg(code);
- m1 = average_cubic(ast, afn);
- code = delete_smaller(&ast, &afn, m1);
- if (code != CORRECT_ENDING) return error_msg(code);
- code = resize_array(&afn, len(ast, afn));
- if (code != CORRECT_ENDING) return error_msg(code);
- output_array(ast, afn);
- m2 = find_min(ast, afn);
- printf("Enter pos of insertion:\n");
- code = scanf("%d", &pos);
- if (code != 1) return SCANNING_FAILURE;
- code = insert_on_pos(&ast, &afn, pos, m2);
- if (code != CORRECT_ENDING) return error_msg(code);
- output_array(ast, afn);
- return CORRECT_ENDING;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement