Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits.h> // Для использования INT_MAX
- using namespace std;
- int main() {
- int N, M;
- cout << "Введите количество строк (N): ";
- cin >> N;
- cout << "Введите количество столбцов (M): ";
- cin >> M;
- int A[N][M];
- cout << "Введите элементы массива " << N << "x" << M << ":" << endl;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- cin >> A[i][j];
- }
- }
- cout << "Исходный массив:" << endl;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- int minPositive = INT_MAX;
- bool hasPositive = false;
- // Найти минимальный положительный элемент
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- if (A[i][j] > 0 && A[i][j] < minPositive) {
- minPositive = A[i][j];
- hasPositive = true;
- }
- }
- }
- if (!hasPositive) {
- cout << "Положительных элементов в массиве нет." << endl;
- return 0;
- }
- bool zeroFound = false;
- // Заменить нулевые элементы
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- if (A[i][j] == 0) {
- A[i][j] = minPositive;
- zeroFound = true;
- }
- }
- }
- // Если нулевых элементов не было, заменить первую строку
- if (!zeroFound) {
- for (int j = 0; j < M; j++) {
- A[0][j] = minPositive;
- }
- }
- cout << "Преобразованный массив:" << endl;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement