AdrianMadajewski

Untitled

May 18th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. inline double f0(int value) {
  6.     return tan((double)value + 12.28);
  7. }
  8.  
  9. inline double f1(int value) {
  10.     return (double)value - 7.92;
  11. }
  12.  
  13. inline double f2(int value) {
  14.     return sqrt(pow((double)value, 4) + 3.32);
  15. }
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     const int ROWS = 2;
  20.     unsigned int K;
  21.     scanf_s("%u", &K);
  22.  
  23.     // Allocate memory and make each R[i, j]
  24.     int **R;
  25.     R = (int**)malloc(ROWS * sizeof *R);
  26.     for(int i = 0; i < ROWS; ++i) {
  27.         R[i] = (int*)malloc(K * sizeof *R[i]);
  28.         for(int j = 0; j < K; ++j) {
  29.             scanf_s("%d", &R[i][j]);
  30.         }
  31.     }
  32.    
  33.     // Allocate S array to store results
  34.     double* S = (double*)malloc(sizeof(double) * K);
  35.  
  36.     // Create array for functions pointers
  37.     double (*functions[])(int) = { f0, f1, f2 };
  38.  
  39.     // Iterate over R to set elements of S
  40.     for (int m = 0; m < K; ++m) {
  41.         int value = R[0][m];
  42.         int passValue = R[1][m];
  43.  
  44.         switch (value) {
  45.         case 0:
  46.             S[m] = functions[0](passValue);
  47.             break;
  48.         case 1:
  49.             S[m] = functions[1](passValue);
  50.             break;
  51.         case 2:
  52.             S[m] = functions[2](passValue);
  53.             break;
  54.         default:
  55.             S[m] = R[1][m];
  56.             break;
  57.         }
  58.     }
  59.  
  60.     // Print results to the user
  61.     for (int i = 0; i < K; ++i) {
  62.         printf("S[%d] = %f\n", i, S[i]);
  63.     }
  64.  
  65.     // Free memory
  66.     for(int i = 0; i < ROWS; ++i) {
  67.         free(R[i]);
  68.     }
  69.     free(R);
  70.     free(S);
  71.  
  72.     return 0;
  73. }
Add Comment
Please, Sign In to add comment