Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <ctime> //Библиотека времени. Использую для работы рандома (srand).
- #include <cstdlib> //Библиотека, в которой содержится сам srand.
- using namespace std;
- //Прототип функции-шаблона выделения памяти под новый массив.
- template <typename TYPE>
- TYPE **newMas(size_t x, size_t y, TYPE flag);
- //Прототип функции-шаблона освобождения памяти от массива.
- template <typename TYPE>
- void delMas(TYPE **mas, size_t x);
- //Прототип функции-шаблона инициализации массива рандомными значениями.
- template <typename TYPE>
- void masRand(TYPE **mas, size_t x, size_t y);
- //Прототип функции-шаблона вывода массива на экран.
- template <typename TYPE>
- void coutMas(TYPE **mas, size_t x, size_t y);
- int main()
- {
- srand(time(NULL)); // Включаем рандом.
- setlocale(0, ""); // Русский язык.
- cout << "Введите размеры массивов" << endl;
- size_t x, y;
- cin >> x;
- cin >> y;
- // Блок выделения памяти под все массивы.
- int **mas1 = newMas(x, y, (int)1);
- int **mas2 = newMas(x, y, (int)1);
- int **minMas = newMas(x, y, (int)1);
- int **maxMas = newMas(x, y, (int)1);
- // Блок инициализации массива рандомными значениями и вывода этого массива на экран.
- masRand(mas1, x, y);
- masRand(mas2, x, y);
- cout << endl << "Первый массив:" << endl;
- coutMas(mas1, x, y);
- cout << endl << "Второй массив:" << endl;
- coutMas(mas2, x, y);
- // Блок сравнений элементов и сортировки в masMin и masMax. После сортировки идет их вывод на экран.
- for (size_t i = 0; i < x; i++)
- for (size_t j = 0; j < y; j++)
- {
- if (mas1[i][j] < mas2[i][j])
- {
- minMas[i][j] = mas1[i][j];
- maxMas[i][j] = mas2[i][j];
- }
- else
- {
- minMas[i][j] = mas2[i][j];
- maxMas[i][j] = mas1[i][j];
- }
- if (mas1[i][j] == mas2[i][j])
- maxMas[i][j] = minMas[i][j] = mas1[i][j];
- }
- cout << endl << "Массив максимальных чисел:" << endl;
- coutMas(maxMas, x, y);
- cout << endl << "Массив минимальных чисел:" << endl;
- coutMas(minMas, x, y);
- // Блок освобождения памяти из под всех массивов.
- delMas(mas1, x);
- delMas(mas2, x);
- delMas(minMas, x);
- delMas(maxMas, x);
- return 0;
- }
- template <typename TYPE>
- TYPE **newMas(size_t x, size_t y, TYPE flag)
- {
- TYPE **mas = new TYPE *[x];
- for (size_t i = 0; i < x; i++)
- mas[i] = new TYPE [y];
- return mas;
- }
- template <typename TYPE>
- void delMas(TYPE **mas, size_t x)
- {
- for (size_t i = 0; i < x; i++)
- delete mas[i];
- delete[]mas;
- }
- template <typename TYPE>
- void masRand(TYPE **mas, size_t x, size_t y)
- {
- for (size_t i = 0; i < x; i++)
- for (size_t j = 0; j < y; j++)
- mas[i][j] = rand() % 51 - 25; //Диапазон значений = -25..25 (0..50 - 25)
- }
- template <typename TYPE>
- void coutMas(TYPE **mas, size_t x, size_t y)
- {
- for (size_t i = 0; i < x; i++)
- {
- for (size_t j = 0; j < y; j++)
- cout << mas[i][j] << "\t";
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement