Advertisement
Bibizon

105c

Oct 3rd, 2022 (edited)
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. // 2.3
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <math.h>
  7.  
  8. int thirdTask()
  9. {
  10.     srand(time(NULL));
  11.     int n, zeros = 0, ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixs = 0, sevens = 0, eights = 0, nines = 0;
  12.     scanf("%d", &n);
  13.     for (int i; i < n; i++)
  14.     {
  15.         int x, xi;
  16.         x = rand() % 10000 + 1;
  17.         xi = (x / 100) % 10;
  18.         printf("%d %d \n", x, xi);
  19.         switch(xi)
  20.         {
  21.             case 0:
  22.                 zeros++;
  23.                 break;
  24.             case 1:
  25.                 ones++;
  26.                 break;
  27.             case 2:
  28.                 twos++;
  29.                 break;
  30.             case 3:
  31.                 threes++;
  32.                 break;
  33.             case 4:
  34.                 fours++;
  35.                 break;
  36.             case 5:
  37.                 fives++;
  38.                 break;
  39.             case 6:
  40.                 sixs++;
  41.                 break;
  42.             case 7:
  43.                 sevens++;
  44.                 break;
  45.             case 8:
  46.                 eights++;
  47.                 break;
  48.             case 9:
  49.                 nines++;
  50.                 break;
  51.             default:
  52.                 break;
  53.         }
  54.     }
  55.     printf("0 = %d, 1 = %d, 2 = %d, 3 = %d, 4 = %d, 5 = %d, 6 = %d, 7 = %d, 8 = %d, 9 = %d", zeros, ones, twos, threes, fours, fives, sixs, sevens, eights, nines);
  56.     return 0;
  57. }
  58.  
  59. // 3.2
  60.  
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <time.h>
  64. #include <math.h>
  65.  
  66. float f(float x)
  67. {
  68.     float y;
  69.     y = pow(cos(x), 2);
  70.     return y;
  71. }
  72.  
  73. int main()
  74. {
  75.     FILE *file;
  76.     file = fopen("function_values.txt", "w");
  77.     float h, left_border, right_border;
  78.     int i1, i2;
  79.     printf("input step, left border, right border\n");
  80.     scanf("%f %f %f", &h, &left_border, &right_border);
  81.     i1 = (int)(left_border / h);
  82.     i2 = (int)(right_border / h);
  83.     printf("\tx_i\t\tf(x_i)\n");
  84.     fprintf(file, "\tx_i\t\tf(x_i)\n");
  85.     for (int i = i1; i < i2; i++)
  86.     {
  87.         float fxi, x = (float)i * h;
  88.         fxi = f(x);
  89.         printf("\t%f\t%f\n", x, fxi);
  90.         fprintf(file, "\t%f\t%f\n", x, fxi);
  91.     }
  92. }
  93.  
  94. // 3.3
  95.  
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include <time.h>
  99. #include <math.h>
  100.  
  101. float f(float x)
  102. {
  103.     float y;
  104.     y = pow(cos(x), 2);
  105.     return y;
  106. }
  107.  
  108. int main()
  109. {
  110.     FILE *file;
  111.     file = fopen("function_values.txt", "w");
  112.     float h, left_border, right_border;
  113.     float function_values[100000][3];
  114.     int i1, i2;
  115.     printf("input step, left border, right border\n");
  116.     scanf("%f %f %f", &h, &left_border, &right_border);
  117.     i1 = (int)(left_border / h);
  118.     i2 = (int)(right_border / h);
  119.     printf("\tx_i\t\tf(x_i)\n");
  120.     fprintf(file, "x_i\tf(x_i)\tf'(x_i)\n");
  121.     for (int i = i1; i < i2; i++)
  122.     {
  123.         float fxi, derivativei, x = (float)i * h;
  124.         fxi = f(x);
  125.         derivativei = (f(x - h) - f(x + h)) / (2 * h);
  126.         function_values[i][0] = x;
  127.         function_values[i][1] = fxi;
  128.         function_values[i][2] = derivativei;
  129.         printf("%f\t%f\t%f\n", x, fxi, derivativei);
  130.         fprintf(file, "%f\t%f\t%f\n", x, fxi, derivativei);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement