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

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

#include <iostream>

#include <limits>

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

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

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

return;

}

int* columnProducts = new int[numCols];

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

int product = 1;

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

product *= matrix[i][j];

}

columnProducts[j] = product;

std::cout << "Product of column " << j + 1 << ": " << product << std::endl;

}

int minProductIndex = 0;

int maxProductIndex = 0;

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

if (columnProducts[j] < columnProducts[minProductIndex]) {

minProductIndex = j;

}

if (columnProducts[j] > columnProducts[maxProductIndex]) {

maxProductIndex = j;

}

}

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

int temp = matrix[i][minProductIndex];

matrix[i][minProductIndex] = matrix[i][maxProductIndex];

matrix[i][maxProductIndex] = temp;

}

std::cout << "Columns 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[] columnProducts;

}

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];

}

}

findProductOfColumns(matrix, numRows, numCols);

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

delete[] matrix[i];

}

delete[] matrix;

return 0;

}

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