Advertisement
SteelK

Untitled

Jun 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  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.     int **mas1 = newMas(x, y, (int)1);
  31.     int **mas2 = newMas(x, y, (int)1);
  32.     int **minMas = newMas(x, y, (int)1);
  33.     int **maxMas = newMas(x, y, (int)1);
  34.  
  35.     masRand(mas1, x, y);
  36.     masRand(mas2, x, y);
  37.     cout << endl << "Первый массив:" << endl;
  38.     coutMas(mas1, x, y);
  39.     cout << endl << "Второй массив:" << endl;
  40.     coutMas(mas2, x, y);
  41.  
  42.     for (size_t i = 0; i < x; i++)
  43.         for (size_t j = 0; j < y; j++)
  44.         {
  45.             if (mas1[i][j] < mas2[i][j])
  46.             {
  47.                 minMas[i][j] = mas1[i][j];
  48.                 maxMas[i][j] = mas2[i][j];
  49.             }
  50.             else
  51.             {
  52.                 minMas[i][j] = mas2[i][j];
  53.                 maxMas[i][j] = mas1[i][j];
  54.             }
  55.  
  56.             if (mas1[i][j] == mas2[i][j])
  57.                 maxMas[i][j] = minMas[i][j] = mas1[i][j];
  58.         }
  59.     cout << endl << "Массив максимальных чисел:" << endl;
  60.     coutMas(maxMas, x, y);
  61.     cout << endl << "Массив минимальных чисел:" << endl;
  62.     coutMas(minMas, x, y);
  63.  
  64.     delMas(mas1, x);
  65.     delMas(mas2, x);
  66.     delMas(minMas, x);
  67.     delMas(maxMas, x);
  68.     return 0;
  69. }
  70.  
  71.  
  72. template <typename TYPE>
  73. TYPE **newMas(size_t x, size_t y, TYPE flag)
  74. {
  75.     TYPE **mas = new TYPE *[x];
  76.     for (size_t i = 0; i < x; i++)
  77.         mas[i] = new TYPE [y];
  78.     return mas;
  79. }
  80.  
  81.  
  82. template <typename TYPE>
  83. void delMas(TYPE **mas, size_t x)
  84. {
  85.     for (size_t i = 0; i < x; i++)
  86.         delete mas[i];
  87.     delete[]mas;
  88. }
  89.  
  90.  
  91. template <typename TYPE>
  92. void masRand(TYPE **mas, size_t x, size_t y)
  93. {
  94.     for (size_t i = 0; i < x; i++)
  95.         for (size_t j = 0; j < y; j++)
  96.             mas[i][j] = rand() % 51 - 25;
  97. }
  98.  
  99.  
  100. template <typename TYPE>
  101. void coutMas(TYPE **mas, size_t x, size_t y)
  102. {
  103.     for (size_t i = 0; i < x; i++)
  104.     {
  105.         for (size_t j = 0; j < y; j++)
  106.             cout << mas[i][j] << "\t";
  107.         cout << endl;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement