C++, Транспонированная матрица

Не хватало транспонированной матрицы:

Upd: Транспонированная матрица на java

Способ без использования динамической памяти (n не больше ста):

#include <iostream>
#include <time.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    const int maxsize = 100;
    int a [maxsize][maxsize], n;
    int temp;
    cout << "n=";
    cin >> n;

    if (n > maxsize)
    {
        cout << "n is too lage...";
        system("pause");
        return -1;
    }
    srand(time(NULL));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            a[i][j] = rand() % 10;
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    for(int i=0;i<n;i++) 
    { 
        for(int j=i;j<n;j++) 
        {
            temp=a[j][i];
            a[j][i]=a[i][j];
            a[i][j]=temp;
        }
    }
    cout << "Transp.:" << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}

Пример работы программы

Добавить комментарий