AdrianMadajewski

Untitled

May 28th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <stdbool.h>    // for bool
  5.  
  6. // Check if the doubles are equal with 'eps' approximation
  7. bool sameDouble(double first, double second, double eps) {
  8.     return fabs(first - second) < eps;
  9. }
  10.  
  11. double f0(int value) {
  12.     double x = (double)value + 12.28;
  13.     double eps = 0.000001;
  14.  
  15.     // Check for bad parameter
  16.     if (sameDouble(cos(x), 0.0000000000, eps)) {
  17.         return -1;
  18.     }
  19.  
  20.     return tan(x);
  21. }
  22.  
  23. double f1(int value) {
  24.     return (double)value - 7.92;
  25. }
  26.  
  27. double f2(int value) {
  28.     return sqrt(pow((double)value, 4) + 3.32);
  29. }
  30.  
  31. int getUserInput(int *input, int INPUT_MIN, int INPUT_MAX) {
  32.     int result = scanf_s("%d", input);
  33.     if (result == 1 && *input >= INPUT_MIN && *input <= INPUT_MAX) {
  34.         return 0;
  35.     }
  36.  
  37.     return -1;
  38. }
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42.     // Constants
  43.     const int ROWS = 2;
  44.     const int MIN_K = 1;
  45.     const int MAX_INT = 1000000;
  46.     const char* errorMessage = "ERROR - USER INPUT\n";
  47.     const int ERROR = -1;
  48.  
  49.     int K;
  50.  
  51.     printf_s("K = ");
  52.     if (getUserInput(&K, MIN_K, MAX_INT) == -1) {
  53.         printf_s(errorMessage);
  54.         return ERROR;
  55.     }
  56.  
  57.     // Allocate memory and make each R[i, j]
  58.     int **R;
  59.     R = (int**)malloc(ROWS * sizeof *R);
  60.     for(int row = 0; row < ROWS; ++row) {
  61.         R[row] = (int*)malloc(K * sizeof *R[row]);
  62.         for(int col = 0; col < K; ++col) {
  63.  
  64.             printf_s("R[%d][%d] = ", row, col);
  65.  
  66.             // Provided bad input
  67.             if (scanf_s("%d", &R[row][col]) != 1) {
  68.                 printf_s(errorMessage);
  69.  
  70.                 // Free memory (error occured on row-th itteration)
  71.                 for (int f = 0; f < row; ++f) {
  72.                     free(R[f]);
  73.                 }
  74.                 free(R);
  75.                
  76.                 // Quit main with ERROR_CODE -1
  77.                 return ERROR;
  78.             }
  79.         }
  80.     }
  81.    
  82.     // Allocate S array to store results
  83.     double* S = (double*)malloc(sizeof(double) * K);
  84.  
  85.     // Create static array for functions pointers
  86.     double (*functions[])(int) = { f0, f1, f2 };
  87.  
  88.     int value;
  89.     int passValue;
  90.  
  91.     // Iterate over R to set elements of S
  92.     for (int m = 0; m < K; ++m) {
  93.         value = R[0][m];
  94.         passValue = R[1][m];
  95.  
  96.         switch (value) {
  97.         case 0:
  98.             S[m] = functions[0](passValue);
  99.             break;
  100.         case 1:
  101.             S[m] = functions[1](passValue);
  102.             break;
  103.         case 2:
  104.             S[m] = functions[2](passValue);
  105.             break;
  106.         default:
  107.             S[m] = R[1][m];
  108.             break;
  109.         }
  110.     }
  111.  
  112.     // Print results to the user
  113.     for (int i = 0; i < K; ++i) {
  114.         printf_s("S[%d] = %f\n", i, S[i]);
  115.     }
  116.  
  117.     // Free memory
  118.     for(int i = 0; i < ROWS; ++i) {
  119.         free(R[i]);
  120.     }
  121.     free(R);
  122.     free(S);
  123.  
  124.     return 0;
  125. }
Add Comment
Please, Sign In to add comment