Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
16.06.2026
Размер:
15.98 Кб
Скачать

13. Написать функцию для нахождения произведения элементов для каждой строки двумерного динамического массива. Поменять местами строки с максимальным и минимальным значением произведения.

#include <iostream>

#include <limits>

void findProductOfRows(int** matrix, int numRows, int numCols) {

if (numRows <= 0 || numCols <= 0) {

std::cout << "Invalid matrix dimensions." << std::endl;

return;

}

int* rowProducts = new int[numRows];

for (int i = 0; i < numRows; ++i) {

int product = 1;

for (int j = 0; j < numCols; ++j) {

product *= matrix[i][j];

}

rowProducts[i] = product;

std::cout << "Product of row " << i + 1 << ": " << product << std::endl;

}

int minProductIndex = 0;

int maxProductIndex = 0;

for (int i = 1; i < numRows; ++i) {

if (rowProducts[i] < rowProducts[minProductIndex]) {

minProductIndex = i;

}

if (rowProducts[i] > rowProducts[maxProductIndex]) {

maxProductIndex = i;

}

}

for (int j = 0; j < numCols; ++j) {

int temp = matrix[minProductIndex][j];

matrix[minProductIndex][j] = matrix[maxProductIndex][j];

matrix[maxProductIndex][j] = temp;

}

std::cout << "Rows with min and max products are swapped." << std::endl;

std::cout << "Updated matrix:" << std::endl;

for (int i = 0; i < numRows; ++i) {

for (int j = 0; j < numCols; ++j) {

std::cout << matrix[i][j] << " ";

}

std::cout << std::endl;

}

delete[] rowProducts;

}

int main() {

int numRows, numCols;

std::cout << "Enter the number of rows: ";

std::cin >> numRows;

std::cout << "Enter the number of columns: ";

std::cin >> numCols;

int** matrix = new int*[numRows];

for (int i = 0; i < numRows; ++i) {

matrix[i] = new int[numCols];

}

std::cout << "Enter the elements of the matrix:" << std::endl;

for (int i = 0; i < numRows; ++i) {

for (int j = 0; j < numCols; ++j) {

std::cin >> matrix[i][j];

}

}

findProductOfRows(matrix, numRows, numCols);

for (int i = 0; i < numRows; ++i) {

delete[] matrix[i];

}

delete[] matrix;

return 0;

}

Соседние файлы в папке Двумерные динамические Массивы ( Решение)