#include <iostream>
#include <time.h>
#include <algorithm>
#include <vector>
#include <variant>
using namespace std;
pair<int**, vector<int>> sum_and_sort(int** mat, int n) {
int d1sum = 0, d2sum = 0;
vector<int> arr;
for (size_t i = 0; i < n; i++)
arr.push_back(mat[i][0]);
for (const auto& i : arr)
sort(arr.begin(), arr.end(), greater <>());
mat[i][0] = arr[i];
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
{
if (i == j)
d1sum += mat[i][j];
if (i + j == (n - 1))
d2sum += mat[i][j];
}
vector<int> arr2;
arr2.push_back(d1sum);
arr2.push_back(d2sum);
return make_pair(mat, arr2);
void printMat(int** mat, int n)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
int main()
srand(time(0));
setlocale(LC_ALL, "Russian");
int n;
cout << "Введите максимальный мазмер матрицы\n";
cin >> n;
int** matrix = new int* [n];
int** matrix2 = new int* [n];
vector<int> sum_d1_d2;
pair<int**, vector<int>> returnf;
for (int i = 0; i < n; ++i) {
matrix[i] = new int[n];
matrix2[i] = new int[n];
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++)
matrix[i][j] = rand() % 100 + 1;
printMat(matrix, n);
returnf = sum_and_sort(matrix,n);
matrix2 = returnf.first;
sum_d1_d2 = returnf.second;
printMat(matrix2, n);
cout << "Сумма диагоналей после сортировки:\n";
for (int i = 0; i < sum_d1_d2.size(); i++) {
cout << sum_d1_d2[i] << " ";
return 1;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <time.h>
#include <algorithm>
#include <vector>
#include <variant>
using namespace std;
pair<int**, vector<int>> sum_and_sort(int** mat, int n) {
int d1sum = 0, d2sum = 0;
vector<int> arr;
for (size_t i = 0; i < n; i++)
arr.push_back(mat[i][0]);
for (const auto& i : arr)
sort(arr.begin(), arr.end(), greater <>());
for (size_t i = 0; i < n; i++)
mat[i][0] = arr[i];
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)
{
if (i == j)
d1sum += mat[i][j];
if (i + j == (n - 1))
d2sum += mat[i][j];
}
vector<int> arr2;
arr2.push_back(d1sum);
arr2.push_back(d2sum);
return make_pair(mat, arr2);
}
void printMat(int** mat, int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
int main()
{
srand(time(0));
setlocale(LC_ALL, "Russian");
int n;
cout << "Введите максимальный мазмер матрицы\n";
cin >> n;
int d1sum = 0, d2sum = 0;
int** matrix = new int* [n];
int** matrix2 = new int* [n];
vector<int> sum_d1_d2;
pair<int**, vector<int>> returnf;
for (int i = 0; i < n; ++i) {
matrix[i] = new int[n];
matrix2[i] = new int[n];
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++)
matrix[i][j] = rand() % 100 + 1;
}
printMat(matrix, n);
returnf = sum_and_sort(matrix,n);
matrix2 = returnf.first;
sum_d1_d2 = returnf.second;
cout << endl;
printMat(matrix2, n);
cout << "Сумма диагоналей после сортировки:\n";
for (int i = 0; i < sum_d1_d2.size(); i++) {
cout << sum_d1_d2[i] << " ";
}
return 1;
}