La transposition d'une matrice en C++

Je suis en train d'écrire un programme pour transposer une matrice donnée à l'aide de la mémoire allouée. La fonction fonctionne parfaitement avec matrice carrée NxN (rows==cols), mais il se bloque avec matrice MxN (lignes != cols). S'il vous plaît aider

void transpose(int **matrix, int *row, int *col)
{
    //dynamically allocate an array
    int **result;
    result = new int *[*col]; //creates a new array of pointers to int objects
    //check for error
    if (result == NULL)
    {
        cout << "Error allocating array";
        exit(1);
    }
    for (int count = 0; count < *col; count++)
    {
        *(result + count) = new int[*row];
    }

    //transposing
    for (int i = 0; i<*row; i++)
    {
       for (int j = i+1; j<*col; j++)
       {
        int temp = *(*(matrix + i) + j);
        *(*(matrix + i) + j) = *(*(matrix + j) + i);
        *(*(matrix + j) + i) = temp;
       }
    }

    for (int i = 0; i<*row; i++)
    {
       for (int j = 0; j<*col; j++)
       {
          *(*(result + i) + j) = *(*(matrix + i) + j);
          cout << *(*(result + i) + j) << "\t";
       }
       cout << endl;
    }
}
new lève une exception en cas d'échec. Utilisation new(nothrow) si vous le souhaitez à renvoyer null en cas d'échec (même si c'est rare de vouloir que).

OriginalL'auteur Casper | 2013-02-13