Отражение элементов матрицы относительно побочной диагонали

Всем здравствуйте. Имеется программа, которая формирует матрицу. Как в функции сделать вывод матрицы, в которой элементы будут отражены относительно побочной диагонали?

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

void Conclusion(int** a, int m, int n) {
    int i, j;
    cout << "Исходная матрица: " << endl;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            cout << setw(4) << a[i][j] << " ";
        cout << endl;
    }
}
void Composition(int** a, int m, int n) {
    int i, j, c = 5;
    long double temp = 1;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            if (a[i][j] < 0) temp *= a[i][j];
            if (a[i][j] >= 0) {
                temp = 1;
                c += 1;
            }
        }
        if (c == 5) {
            cout << "Номер строки: " << i+1 << " = " << temp << endl;
            temp = 1;
            c = 5;
        }
        else {
            cout << "Номер строки: " << i+1 << " = " << "Не все элементы отрицательные" << endl;
            temp = 1;
            c = 5;
        }
    }
}
void Replacement(int** a, int m, int n) {
    int i, j;
    cout << "Изменённая матрица: " << endl;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            if (a[i][j] >= 0) {
                cout << setw(4) << -1 << " ";
            }
            else
                cout << setw(4) << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main() {
    setlocale(LC_ALL, "rus");
    srand(time(0));
    int m, n, i, j;
    cout << "Введите количество строк и столбцов матрицы: ";
    cin >> m >> n;
    int** a = new int* [m];
    for (i = 0; i < m; i++)
        a[i] = new int[n];
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            a[i][j] = -40 + rand() % 50;
    Conclusion(a, m, n);
    Composition(a, m, n);
    Replacement(a, m, n);
    for (int i = 0; i < m; i++)
        delete[]a[i];
    delete[]a;
    a = NULL;
    return 0;
}

Ответы (1 шт):

Автор решения: Miracle-
#include <iostream>
#include <cstddef>
#include <algorithm>
#include <ctime>

void Swap(int** arr, int size)
{
    for (std::size_t i = 0; i < size - 1; ++i)
    {
        for (std::size_t j = 0; j < size - i - 1; ++j)
        {
            std::swap(arr[i][j], arr[size - 1 - j][size - 1 - i]);
        }
    }
}


int main()
{
    std::srand(std::time(0));
    constexpr int SIZE = 3;
    int** arr = new int* [SIZE];
    for (std::size_t i = 0; i < SIZE; ++i)
    {
        arr[i] = new int[SIZE];
        for (std::size_t j = 0; j < SIZE; ++j)
        {
            arr[i][j] = -40 + rand() % 50;
            std::cout << arr[i][j] << " ";
        }
        std::cout << '\n';
    }

    Swap(arr, SIZE);
    std::cout << '\n';

    for (std::size_t i = 0; i < SIZE; ++i)
    {
        for (std::size_t j = 0; j < SIZE; ++j)
            std::cout << arr[i][j] << " ";
        std::cout << '\n';
        delete[] arr[i];
    }

    delete[] arr;
}

UPD-> Почему size - 1 - j?. Попробуйте поразмыслить взяв ручку ,блокнот и представить это на бумаге:

Допустим arr[size][size] ={{5,4,3},{8,9,15},{1,7,11}};,тогда:

arr[i][j] == arr[0][0] -> 5;
arr[size - 1 - j][size - 1 - i] == arr[2][2] -> 11;

Меняем их местами. Дальше все то же самое.

→ Ссылка