Advertisement
SteelK

Untitled

Jun 15th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime> //Библиотека времени. Использую для работы рандома (srand).
  4. #include <cstdlib> //Библиотека, в которой содержится сам srand.
  5.  
  6. using namespace std;
  7. //Прототип функции-шаблона выделения памяти под новый массив.
  8. template <typename TYPE>
  9. TYPE **newMas(size_t x, size_t y, TYPE flag);
  10. //Прототип функции-шаблона освобождения памяти от массива.
  11. template <typename TYPE>
  12. void delMas(TYPE **mas, size_t x);
  13. //Прототип функции-шаблона инициализации массива рандомными значениями.
  14. template <typename TYPE>
  15. void masRand(TYPE **mas, size_t x, size_t y);
  16. //Прототип функции-шаблона вывода массива на экран.
  17. template <typename TYPE>
  18. void coutMas(TYPE **mas, size_t x, size_t y);
  19.  
  20. int main()
  21. {
  22.     srand(time(NULL)); // Включаем рандом.
  23.     setlocale(0, ""); // Русский язык.
  24.  
  25.     cout << "Введите размеры массивов" << endl;
  26.     size_t x, y;
  27.     cin >> x;
  28.     cin >> y;
  29.  
  30.     // Блок выделения памяти под все массивы.
  31.     int **mas1 = newMas(x, y, (int)1);
  32.     int **mas2 = newMas(x, y, (int)1);
  33.     int **minMas = newMas(x, y, (int)1);
  34.     int **maxMas = newMas(x, y, (int)1);
  35.  
  36.     // Блок инициализации массива рандомными значениями и вывода этого массива на экран.
  37.     masRand(mas1, x, y);
  38.     masRand(mas2, x, y);
  39.     cout << endl << "Первый массив:" << endl;
  40.     coutMas(mas1, x, y);
  41.     cout << endl << "Второй массив:" << endl;
  42.     coutMas(mas2, x, y);
  43.  
  44.     // Блок сравнений элементов и сортировки в masMin и masMax. После сортировки идет их вывод на экран.
  45.     for (size_t i = 0; i < x; i++)
  46.         for (size_t j = 0; j < y; j++)
  47.         {
  48.             if (mas1[i][j] < mas2[i][j])
  49.             {
  50.                 minMas[i][j] = mas1[i][j];
  51.                 maxMas[i][j] = mas2[i][j];
  52.             }
  53.             else
  54.             {
  55.                 minMas[i][j] = mas2[i][j];
  56.                 maxMas[i][j] = mas1[i][j];
  57.             }
  58.  
  59.             if (mas1[i][j] == mas2[i][j])
  60.                 maxMas[i][j] = minMas[i][j] = mas1[i][j];
  61.         }
  62.     cout << endl << "Массив максимальных чисел:" << endl;
  63.     coutMas(maxMas, x, y);
  64.     cout << endl << "Массив минимальных чисел:" << endl;
  65.     coutMas(minMas, x, y);
  66.    
  67.     // Блок освобождения памяти из под всех массивов.
  68.     delMas(mas1, x);
  69.     delMas(mas2, x);
  70.     delMas(minMas, x);
  71.     delMas(maxMas, x);
  72.     return 0;
  73. }
  74.  
  75.  
  76. template <typename TYPE>
  77. TYPE **newMas(size_t x, size_t y, TYPE flag)
  78. {
  79.     TYPE **mas = new TYPE *[x];
  80.     for (size_t i = 0; i < x; i++)
  81.         mas[i] = new TYPE [y];
  82.     return mas;
  83. }
  84.  
  85.  
  86. template <typename TYPE>
  87. void delMas(TYPE **mas, size_t x)
  88. {
  89.     for (size_t i = 0; i < x; i++)
  90.         delete mas[i];
  91.     delete[]mas;
  92. }
  93.  
  94.  
  95. template <typename TYPE>
  96. void masRand(TYPE **mas, size_t x, size_t y)
  97. {
  98.     for (size_t i = 0; i < x; i++)
  99.         for (size_t j = 0; j < y; j++)
  100.             mas[i][j] = rand() % 51 - 25; //Диапазон значений = -25..25 (0..50 - 25)
  101. }
  102.  
  103.  
  104. template <typename TYPE>
  105. void coutMas(TYPE **mas, size_t x, size_t y)
  106. {
  107.     for (size_t i = 0; i < x; i++)
  108.     {
  109.         for (size_t j = 0; j < y; j++)
  110.             cout << mas[i][j] << "\t";
  111.         cout << endl;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement