Advertisement
ProgNeo

Untitled

Sep 17th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. int max(int* array, int n)
  8. {
  9. int max = array[0];
  10. for (int i = 1; i < n; i++)
  11. {
  12. if (max < array[i])
  13. {
  14. max = array[i];
  15. }
  16. }
  17. return max;
  18. }
  19.  
  20. int intInput(char ch, int index)
  21. {
  22. int a;
  23. cout << ch << '[' << index << "] = ";
  24. while (!(cin >> a) || (cin.peek() != '\n'))
  25. {
  26. cin.clear();
  27. while (cin.get() != '\n')
  28. {
  29. cout << "Неверный ввод!";
  30. }
  31. }
  32. return a;
  33. }
  34.  
  35. int intRandom()
  36. {
  37. return int(-100 + rand() % (100 + 100));
  38. }
  39.  
  40. int* getArray(int n, char ch, bool random)
  41. {
  42. int* arr = new int[n];
  43. for (int i = 0; i < n; i++)
  44. {
  45. if (random != true)
  46. arr[i] = intInput(ch, i);
  47. else
  48. arr[i] = intRandom();
  49. }
  50. return arr;
  51. }
  52.  
  53. int* defineTArray(int* X, int* Y, int* Z, int n)
  54. {
  55. int* T = new int[10];
  56. int maxX = max(X, 10);
  57. int maxY = max(Y, 10);
  58. int maxZ = max(Z, 10);
  59. for (int i = 0; i < n; i++)
  60. {
  61. if (Z[i] > 0)
  62. {
  63. T[i] = (maxX + maxY) / 2;
  64. }
  65. else
  66. {
  67. T[i] = 1 + (maxZ * maxZ);
  68. }
  69. }
  70. return T;
  71. }
  72.  
  73. void print(int* arr, int n, char ch)
  74. {
  75. cout << ch << " = ";
  76. for (int i = 0; i < n; i++)
  77. {
  78. cout << arr[i] << ' ';
  79. }
  80. }
  81.  
  82. int main()
  83. {
  84. setlocale(LC_ALL, "rus");
  85. srand(time(0));
  86. bool random;
  87.  
  88. cout << "Заполнение массивов случайными числами - 1, вручную - 0: ";
  89. while (!(cin >> random) || (cin.peek() != '\n')) {
  90. cin.clear();
  91. while (cin.get() != '\n');
  92. cout << "Заполнение массива случайными числами - 1, вручную - 0: ";
  93. }
  94.  
  95. int* X = getArray(10, 'X', random);
  96. int* Y = getArray(10, 'Y', random);
  97. int* Z = getArray(10, 'Z', random);
  98. int* T = defineTArray(X, Y, Z, 10);
  99. print(T, 10, 'T');
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement