Другие языки программирования и технологии

C++ Как найти сумму и произведение элементов K - го столбца данной матрицы?

*sum = 0;
*prod = 1;

for (int j = 0; j < n; j++) {
*sum += x[k][j];
*prod *= x[k][j];
}
s=0; prod=1; for(int i=0;i< m;i++){s+=x[i][k]; p*=x[i][k];}
Андрей Андреев
Андрей Андреев
91 201
Лучший ответ
// https://pastebin.com/aegTtLHN

#include "cstdlib"
#include "locale"
#include "iostream"
#include "iomanip"

#define CONST_ABS_ELEM_VALUE 20
#define DEFAULT_VALUE_SETW 5

int** initialize_new_matrix_with_random_values(int n, int m);
void delete_matrix(int** matrix, int m);
void output_full_matrix(int const n, int const m, int** const a, int setw_value);
void init_io_threads();
int** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m);

int main()
{
srand(NULL);
init_io_threads();

auto n = 12;
auto m = 14;

std::wcout << L"Введите кол-во строк: ";
std::cin >> n;
std::wcout << L"Введите кол-во столбцов: ";
std::cin >> m;
std::wcout << std::endl << L"Введена матрица " << n << L" x " << m << L"." << std::endl;
std::wcout << L"Матрица заполняется случайными значениями." << std::endl;

const auto matrix = initialize_new_matrix_with_random_values(n, m);

output_full_matrix(n, m, matrix, DEFAULT_VALUE_SETW);

std::wcout << L"Находим произведение и сумму элементов каждого столбца в матрице." << std::endl;

const auto result = get_multiplication_and_sum_of_lines(matrix, n, m);

std::wcout << L"Выводим полученные значение (1-ый столбец - сумма, 2-ой - произведение)." << std::endl;

output_full_matrix(m, 2, result, DEFAULT_VALUE_SETW * 5);

std::wcout << L"Освобождаем память." << std::endl;

delete_matrix(matrix, n);
delete_matrix(result, m);

system("pause");
}

int** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m)
{
const auto result = new int *[m];

for (auto i = 0; i < m; i++)
{
result[i] = new int[2];

for (auto j = 0; j < 2; j++)
{
result[i][j] = j;
}

for (auto j = 0; j < n; j++)
{
result[i][0] += src_matrix[j][i];
result[i][1] *= src_matrix[j][i];
}
}

return result;
}

int** initialize_new_matrix_with_random_values(const int n, const int m)
{
const auto matrix = new int *[n];

for (auto i = 0; i < n; i++)
{
matrix[i] = new int[m];

for (auto j = 0; j < m; j++)
{
matrix[i][j] = rand() % CONST_ABS_ELEM_VALUE * (rand() % 2 == 1? 1 : -1);
}
}

return matrix;
}

void delete_matrix(int** matrix, const int n)
{
for (auto i = 0; i < n; i++)
{
delete[] matrix[i];
}

delete[] matrix;
}

void init_io_threads()
{
std::ios_base::sync_with_stdio(false);
std::wcout.imbue(std::locale("rus_RUS.866"));
}

void output_full_matrix(int const n, int const m, int** const a, const int setw_value)
{
std::wcout << L"Вывод матрицы:" << std::endl << std::setw(setw_value + 1) << L"";

for (auto i = 0; i < m; i++)
{
std::wcout << std::setw(setw_value) << i + 1;
}

std::wcout << std::endl;

for (auto i = 0; i < n; i++)
{
std::wcout << std::endl << std::setw(setw_value) << i + 1 << L":";
for (auto j = 0; j < m; j++)
{
std::wcout << std::setw(setw_value) << a[i][j];
}
}

std::wcout << std::endl << L"Конец вывода." << std::endl;
}
Д.
Денис .
1 755
Вова Климович Спасибо конечно, но зачем мне нужно было писать целый огромный код?? Мне это нужно не было, я этого не просил, я для кого и для чего свой цикл приписал в дополнении, явно же не для красоты, а что бы его подредактировали, к примеру как сделал этот парень выше.
И в чём вопрос? Какой матрицы?
Вова Климович матрица размера M на N, надо найти для каждого её столбца произведение её элементов

Похожие вопросы