1231

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
2
Indexable
Never
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cout << "Enter the 2size of your matrix:";
    cin >> n;

    int** matrix = new int*[n];
    for (int i = 0; i < n; i++)
    {
        matrix[i] = new int[n];
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << "Enter the matrix elements:";

            cin >> matrix[i][j];
        }
    }

    bool isMatch = false;
    for (int k = 0; k < n; k++)
    {
        if (matrix[k][k] == matrix[k][n - k - 1])
        {
            isMatch = true;
            cout << "Match found at k = " << k << endl;
            break;
        }
    }

    if (!isMatch)
    {
        cout << "No matches found, transposing matrix..." << endl;
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }

    cout << "Resulting matrix: " << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    for (int i = 0; i < n; i++)
    {
        delete[] matrix[i];
    }
    delete[] matrix;

    return 0;
}