Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "cstdlib"
- #include "ctime"
- #include "iostream"
- #include "iomanip"
- #include "locale.h"
- using namespace std;
- double** generate_random_matrix(int n, int random_max = 100)
- {
- srand(time(nullptr));
- double** a = new double*[n];
- for (int i = 0; i < n; i++)
- {
- a[i] = new double[n];
- for (int j = 0; j < n; j++)
- {
- a[i][j] = rand() % random_max;
- }
- }
- return a;
- }
- ////
- void smoothing_matrix(double** const src, int n)
- {
- int **dst = new int*[n];
- for (int i = 0; i < i; i++) {
- int cnt = 0;
- int sum = 0;
- for (int j = 0; j < n; j++) {
- if((i+1)<n){
- sum += src[i + 1][j];
- cnt++;
- }
- if ((j + 1)<n) {
- sum += src[i][j + 1];
- cnt++;
- }
- if ((i - 1)>=0) {
- sum += src[i + 1][j];
- cnt++;
- }
- if ((j - 1) >= 0) {
- sum += src[i][j + 1];
- cnt++;
- }
- dst[i][j] = sum / cnt;
- }
- }
- cout << "Сглаженная матрица: " << endl;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cout << dst[i][j] << " ";//
- }
- cout << endl;
- }
- }
- ////
- double sum_under_diag(double** const a, int const n)
- {
- double sum = .0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < i; j++)
- {
- sum += abs(a[i][j]);
- }
- }
- return sum;
- }
- void output_matrix(double** const a, int const n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << setw(10) << a[i][j];
- }
- cout << endl;
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Rus");
- int n;
- cout << "Введите размер матрицы:";
- cin >> n;
- if (n <= 2)
- {
- cout << "\nВведен некорректный размер матрицы\n";
- cin.get(); cin.get();
- return 0;
- }
- else
- {
- int m_size = n;
- int max_random = 100;
- cout.precision(3);
- double** const source_matrix = generate_random_matrix(m_size, max_random);
- cout << "Матрица: " << endl;
- output_matrix(source_matrix, m_size);
- //cout << "Сглаженная матрица: " << endl;
- smoothing_matrix(source_matrix, m_size);
- //output_matrix(source_matrix, m_size);
- cout << "Сумма элементов под главной диагональю: " << sum_under_diag(source_matrix, m_size) << endl;
- for (int i = 0; i < m_size; i++)
- delete[] source_matrix[i];
- delete[] source_matrix;
- }
- system("pause");
- return 0;
Add Comment
Please, Sign In to add comment