Читаючи матрицю A[ij]m,n по рядкам, елементами якої є цілі числа, побудувати вектор з додатніх елементів, які кратні 3.
c++ помогите пожалуйста
Ответы на вопрос
Ответ:
#include <iostream>
#include <Windows.h>
#include <ctime>
#include <vector>
using namespace std;
int** CreateMatrix(int rows, int cols)
{
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) arr[i] = new int[cols];
return arr;
}
void DeleteMatrix(int** arr, int rows)
{
for (int i = 0; i < rows; i++) delete[] arr[i];
delete[] arr;
}
void FillMatrix(int** arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
arr[i][j] = -20 + rand() % 50;
}
void PrintMatrix(int** arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
cout << arr[i][j] << (j < cols - 1 ? " " : "\n");
}
int main()
{
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int n{}, m{};
cout << "Введіть кількість рядків у матриці: ";
cin >> n;
cout << "Введіть кількість стовпців у матриці: ";
cin >> m;
int** arr = CreateMatrix(n, m);
FillMatrix(arr, n, m);
cout << "Матриця:\n";
PrintMatrix(arr, n, m);
vector<int> vec{};
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (arr[i][j] >= 0 && arr[i][j] % 3 == 0) vec.push_back(arr[i][j]);
cout << "\nВектор: ";
for (int x : vec)
cout << x << " ";
DeleteMatrix(arr, n);
return 0;
}