Cheeel666

Untitled

Oct 6th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double m1(double *arr, int n)
  5. {
  6. double t, arrange_sum = 0;
  7. for (int i = 0; i < n; i++)
  8. {
  9. t = arr[i] / n;
  10. arrange_sum += t;
  11. }
  12.  
  13. return arrange_sum;
  14. }
  15.  
  16. double m2(double *arr1, int n)
  17. {
  18. double max;
  19. max = arr1[0];
  20. for (int i = 1; i < n; i++)
  21. {
  22. if (arr1[i] > max)
  23. max = arr1[i];
  24. }
  25. return max;
  26. }
  27.  
  28. int make_a1(double *arr, int n, double arrange_sum)
  29. {
  30. int count = n;
  31. for (int i = 0; i < n; i++)
  32. {
  33. for (int k = 0; k < n; k++)
  34. printf ("%lf ",arr[k]);
  35. printf("\n");
  36. if ((arr[i] - arrange_sum) > 1e-10)
  37. {
  38. for (int j = i; j < count; j++)
  39. {
  40. arr[j] = arr[j+1];
  41. }
  42. count--;
  43. i--;
  44. }
  45. }
  46.  
  47. return count;
  48. }
  49.  
  50. void make_a2(double *arr, int n, int p, double mu2)
  51. {
  52. int i = p;
  53. double t, t1;
  54. t = arr[p];
  55. arr[p] = mu2;
  56.  
  57. while (i < n + 1)
  58. {
  59. t1 = t;
  60. t = arr[i];
  61. arr[i+1] = t1;
  62.  
  63. i++;
  64. }
  65. }
  66.  
  67. void test_m1()
  68. {
  69. double eps = 1e-5;
  70. double rez;
  71. int err_counter = 0;
  72. // Test 1
  73. double a[5] = {1, 2, 3, 4, 5};
  74. rez = m1(a,5);
  75. if (fabsf(rez - 3)>eps)
  76. err_counter++;
  77.  
  78. // Test 2
  79. double a1[4] = {0, 2, 0, 2};
  80. rez = m1(a1,4);
  81. if (fabsf(rez - 1) > eps)
  82. err_counter++;
  83. // Test 3
  84. double a2[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
  85. rez = m1(a2,5);
  86. if (fabsf(rez - 3.3) > eps)
  87. err_counter++;
  88. printf("Error test of m1 function : %s\n", err_counter ? "FAILED" : "OK");
  89. }
  90.  
  91. void test_m2()
  92. {
  93. double eps = 1e-5;
  94. double rez;
  95. int err_counter = 0;
  96. // Test 1
  97. double a[9] = {1, 2, 3, 4, 5, 4, 3, 2, 1};
  98. rez = m2(a,5);
  99. if (rez != 5)
  100. err_counter++;
  101.  
  102. // Test 2
  103. double a1[4] = {0, 2, 0, 2};
  104. rez = m2(a1,4);
  105. if (rez != 2)
  106. err_counter++;
  107. // Test 3
  108. double a2[5] = {-5.0, -4.2, -3.4, -2.1, 0};
  109. rez = m2(a2,5);
  110. if (rez != 0)
  111. err_counter++;
  112. printf("Error test of m2 function : %s\n", err_counter ? "FAILED" : "OK");
  113.  
  114. }
  115.  
  116. void test_make_a1()
  117. {
  118. double eps = 1e-5;
  119. double rez;
  120. int err_counter = 0;
  121. // Test 1
  122. double a[5] = {1, 2, 3, 4, 5};
  123. rez = make_a1(a,5, 3);
  124. printf("rez = %lf\n", rez);
  125. if (rez != 3)
  126. err_counter++;
  127.  
  128. // Test 2
  129. double a1[4] = {0, 2, 0, 2};
  130. rez = make_a1(a1, 4, 0.5);
  131. if (rez != 2)
  132. err_counter++;
  133. // Test 3
  134. double a2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  135. printf("rez = %lf\n", rez);
  136. rez = make_a1(a2, 10, 5.5);
  137. printf("rez = %lf\n", rez);
  138. if (rez != 5)
  139. err_counter++;
  140. printf("Error test of make_a1 function : %s\n", err_counter ? "FAILED" : "OK");
  141.  
  142. }
  143.  
  144. void test_make_a2()
  145. {
  146.  
  147. printf("OK");
  148. }
  149.  
  150.  
  151. int main()
  152. {
  153. double a[10] = {1,2,3,4,5,6,7,8,9,10};
  154. printf("%lf\n",m1(a,10));
  155. test_m1();
  156. test_m2();
  157. test_make_a1();
  158. test_make_a2();
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment