Пиша програма за транспониране на дадена матрица, използвайки разпределена памет. Функцията работи перфектно с квадратна матрица NxN (редове==колове), но се срива с матрица MxN (редове !=колове). Моля помогнете
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
хвърля изключение при повреда. Използвайтеnew(nothrow)
, ако искате да върнеnull
при повреда (въпреки че е необичайно да искате това). - person Peter Wood   schedule 13.02.2013