C/C++
Программирование алгоритмов с использованием двумерных массивов c++
Задан массив размером n x n, состоящий из 0 и 1. Повернуть элементы массива на 90 градусов против часовой стрелки
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
int matrix[50][50], res[50][50], size;
cout << "Enter a size of the matrix: ";
cin >> size;
cout << "array 1:\n";
srand(time(NULL));
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << setw(4) << (matrix[i][j] = rand() % 2);
}
cout << "\n";
}
cout << "\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
res[size-j-1][i] = matrix[i][j];
}
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix[i][j] = res[i][j];
}
}
cout << "array 2:\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << setw(4) << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
int matrix[50][50], res[50][50], size;
cout << "Enter a size of the matrix: ";
cin >> size;
cout << "array 1:\n";
srand(time(NULL));
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << setw(4) << (matrix[i][j] = rand() % 2);
}
cout << "\n";
}
cout << "\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
res[size-j-1][i] = matrix[i][j];
}
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix[i][j] = res[i][j];
}
}
cout << "array 2:\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << setw(4) << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
#include <algorithm>
#include <random>
#include <iostream>
#include <iomanip>
using namespace std;
size_t setsize(const char* msg) {
cout << msg;
size_t value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
puts("");
return value;
}
int** create(size_t n) {
auto matrix = new int* [n];
for (auto i = 0U; i < n; ++i) {
matrix[i] = new int[n];
}
return matrix;
}
int** destroy(int** matrix, size_t n) {
if (matrix != nullptr) {
for (auto i = 0U; i < n; ++i) {
delete matrix[i];
}
delete matrix;
}
return nullptr;
}
void fill(int** matrix, size_t n) {
uniform_int_distribution<> uid(0, 1);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
for (auto i = 0U; i < n; ++i) {
generate(matrix[i], matrix[i] + n, rand);
}
}
void print(int** matrix, size_t n) {
auto show = [](int x) { cout << setw(3) << x; };
for (auto i = 0U; i < n; ++i) {
for_each(matrix[i], matrix[i] + n, show);
puts("\n");
}
puts("");
}
void rotate90left(int** matrix, size_t n) {
auto tmp = create(n);
for (auto i = 0U; i < n; ++i) {
copy(matrix[i], matrix[i] + n, tmp[i]);
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
matrix[i][j] = tmp[j][n - i - 1];
}
}
tmp = destroy(tmp, n);
}
int main() {
size_t n = setsize("Size: ");
auto matrix = create(n);
fill(matrix, n);
print(matrix, n);
rotate90left(matrix, n);
print(matrix, n);
matrix = destroy(matrix, n);
system("pause > nul");
}
#include <random>
#include <iostream>
#include <iomanip>
using namespace std;
size_t setsize(const char* msg) {
cout << msg;
size_t value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
puts("");
return value;
}
int** create(size_t n) {
auto matrix = new int* [n];
for (auto i = 0U; i < n; ++i) {
matrix[i] = new int[n];
}
return matrix;
}
int** destroy(int** matrix, size_t n) {
if (matrix != nullptr) {
for (auto i = 0U; i < n; ++i) {
delete matrix[i];
}
delete matrix;
}
return nullptr;
}
void fill(int** matrix, size_t n) {
uniform_int_distribution<> uid(0, 1);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
for (auto i = 0U; i < n; ++i) {
generate(matrix[i], matrix[i] + n, rand);
}
}
void print(int** matrix, size_t n) {
auto show = [](int x) { cout << setw(3) << x; };
for (auto i = 0U; i < n; ++i) {
for_each(matrix[i], matrix[i] + n, show);
puts("\n");
}
puts("");
}
void rotate90left(int** matrix, size_t n) {
auto tmp = create(n);
for (auto i = 0U; i < n; ++i) {
copy(matrix[i], matrix[i] + n, tmp[i]);
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < n; ++j) {
matrix[i][j] = tmp[j][n - i - 1];
}
}
tmp = destroy(tmp, n);
}
int main() {
size_t n = setsize("Size: ");
auto matrix = create(n);
fill(matrix, n);
print(matrix, n);
rotate90left(matrix, n);
print(matrix, n);
matrix = destroy(matrix, n);
system("pause > nul");
}
cout << setw(4) << matrix[i][j];
Похожие вопросы
- Двумерные массивы C++, ничего не понимаю((( Нужна помощь
- Двумерный массив C++
- Минимакс двумерного массива, c#
- Cоставление и отладка программ работы с двумерными массивами C++
- Cоставление и отладка программ работы с двумерными массивами. C++
- C++ программирование с использованием динамических двумерных массивов
- Программирование на C++ с использованием динамического массива
- Задача.Программирование.С++.Динамический двумерный массив.
- Заполнить двумерный массив 5*3 и найти строку с максимальным произведением элементов. C++
- Программирование алгоритмов обработки многомерных массивов.