1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
using namespace std;
void printMatrix(int* arr[],int,int);
void transposeMatrix(int* arr[],int,int); //(matrix,rows,columns);
int main(void)
{
cout << "-------------------------\n"
<<"Transpose of a Matrix\n"
<< "-------------------------\n\n";
int i,j; // i = rows; j = columns;
cout << "Specify how many rows compose the matrix (up to 4): ";
cin >> i;
cout << "How many columns (up to 4): ";
cin >> j;
//declaration
int ** matrix;
//allocation
matrix = new int*[i];
for (int n = 0; n < i; n++)
{
matrix[n] = new int[j];
}
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
cout << "Enter " << a << b << ": ";
cin >> matrix[a][b];
}
}
cout << "A:\n";
printMatrix(matrix,i,j);
cout << "Transpose of A:\n";
transposeMatrix(matrix,i,j);
// deallocation
for (int m = 0; m < i; m++)
delete matrix[m];
delete matrix;
return 0;
}
void printMatrix(int* arr[],int i, int j)
{
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
cout << arr[a][b] << " ";
}
cout << "\n";
}
}
void transposeMatrix(int* arr[],int i,int j)
{
//temporary array
int ** temp;
//allocation
temp = new int*[i];
for (int n = 0; n < i; n++)
{
temp[n] = new int[j];
}
for (int a = 0; a < i; a++)
{
for (int b = 0; b < j; b++)
{
temp[a][b] = arr[b][a]; //reverse
}
}
printMatrix(temp,i,j);
//deallocation
for (int m = 0; m < i; m++)
delete temp[m];
delete temp;
}
|