*sum = 0;
*prod = 1;
for (int j = 0; j < n; j++) {
*sum += x[k][j];
*prod *= x[k][j];
}
Другие языки программирования и технологии
C++ Как найти сумму и произведение элементов K - го столбца данной матрицы?
s=0; prod=1; for(int i=0;i< m;i++){s+=x[i][k]; p*=x[i][k];}
// 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;
}
#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;
}
Вова Климович
Спасибо конечно, но зачем мне нужно было писать целый огромный код?? Мне это нужно не было, я этого не просил, я для кого и для чего свой цикл приписал в дополнении, явно же не для красоты, а что бы его подредактировали, к примеру как сделал этот парень выше.
И в чём вопрос? Какой матрицы?
Вова Климович
матрица размера M на N, надо найти для каждого её столбца произведение её элементов
Похожие вопросы
- c++ помогите найти сумму элементов на главной диагонали?
- Помогите пожалуйста написать программу: Найти сумму индексов четных элементов массива. На языке С++.
- Помогите решить задачу по программированию, пожалуйста. Найти сумму наименьших значений элементов строк. (вложенные циклы)
- нужна помощь. найти сумму квадратов чётных элементов на языке ассемблер используя арифметические команды
- дана матрица а размерности n на m. Найти максимальный элемент в каждом столбце. Помогите пожалуйста решить. На языке C++
- Как поменять местами столбцы в матрице. C++
- Найти сумму элементов квадратной матрицы ниже главной и побочной диагонали.. Банктик получается. C++ ,Pascal
- C++. Перевернуть столбцы в матрице!
- В чем ошибка? Помогите разобраться! (программа на С++: добавление столбца в матрицу)
- как решить? Найти сумму элементов прямоугольной матрицы X(n,m), находящихся по периметру этой матрицы. язык: С++