### Задание 2: Двумерные массивы
**a) Функция для заполнения двумерного массива с клавиатуры:**
```cpp
#include <iostream>
using namespace std;
void inputMatrix(int matrix[][5], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Введите элемент [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}
}
int main() {
const int rows = 3, cols = 5;
int matrix[rows][cols];
inputMatrix(matrix, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
---
**b) Функция для вывода прямоугольной матрицы на экран:**
```cpp
#include <iostream>
using namespace std;
void printMatrix(int matrix[][5], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}
int main() {
const int rows = 3, cols = 5;
int matrix[rows][cols] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
printMatrix(matrix, rows, cols);
return 0;
}
```
---
**c) Заполнение матрицы для игры "Сапёр":**
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillMinesweeperMatrix(int matrix[][5], int rows, int cols, int numMines) {
srand(time(0));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = 0;
}
}
while (numMines > 0) {
int i = rand() % rows;
int j = rand() % cols;
if (matrix[i][j] == 0) {
matrix[i][j] = -1;
numMines--;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == -1) continue;
int minesCount = 0;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int ni = i + x, nj = j + y;
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols && matrix[ni][nj] == -1) {
minesCount++;
}
}
}
matrix[i][j] = minesCount;
}
}
}
void printMatrix(int matrix[][5], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == -1)
cout << "*\t";
else
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}
int main() {
const int rows = 5, cols = 5, numMines = 5;
int matrix[rows][cols];
fillMinesweeperMatrix(matrix, rows, cols, numMines);
printMatrix(matrix, rows, cols);
return 0;
}
```
---
**d) Циклический сдвиг строк на одну вниз:**
```cpp
#include <iostream>
using namespace std;
void shiftRowsDown(int matrix[][5], int rows, int cols) {
int temp[cols];
for (int j = 0; j < cols; j++) {
temp[j] = matrix[rows - 1][j];
}
for (int i = rows - 1; i > 0; i--) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = matrix[i - 1][j];
}
}
for (int j = 0; j < cols; j++) {
matrix[0][j] = temp[j];
}
}
void printMatrix(int matrix[][5], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
const int rows = 4, cols = 5;
int matrix[rows][cols] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
cout << "Исходная матрица:" << endl;
printMatrix(matrix, rows, cols);
shiftRowsDown(matrix, rows, cols);
cout << "После циклического сдвига:" << endl;
printMatrix(matrix, rows, cols);
return 0;
}
```
---
**e) Подсчёт количества элементов в строке:**
```cpp
#include <iostream>
using namespace std;
int countOccurrences(int matrix[][5], int row, int cols, int value) {
int count = 0;
for (int j = 0; j < cols; j++) {
if (matrix[row][j] == value) {
count++;
}
}
return count;
}
int main() {
const int rows = 4, cols = 5;
int matrix[rows][cols] = {
{1, 2, 3, 4, 5},
{5, 4, 3, 2, 1},
{1, 1, 1, 1, 1},
{2, 2, 3, 4, 5}
};
int row = 2, value = 1;
cout << "Количество " << value << " в строке " << row << ": "
<< countOccurrences(matrix, row, cols, value) << endl;
return 0;
}
```
---
Если вам нужно продолжить с трехмерными массивами (Задание 3), уточните!