-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq08.cpp
66 lines (52 loc) · 1.46 KB
/
q08.cpp
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
/*Program to find transpose of a matrix */
#include <iostream>
using namespace std;
int ** allocate_matrix (int rows, int cols);
void display_matrix (int **matrix, int rows, int cols);
int ** allocate_matrix (int rows, int cols) {
int **matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
return matrix;
}
void dealloc_matrix (int **matrix, int rows) {
for (int i = 0; i < rows; i++) {
delete [] matrix[i];
}
delete [] matrix;
}
void display_matrix (int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
cout << matrix[i][j] << " \t";
}
cout << endl;
}
}
int main() {
int **matrix, **transpose;
int rows, cols;
cout << "Enter details of matrix: " << endl;
cout << "Rows: ";
cin >> rows;
cout << "Columns: ";
cin >> cols;
matrix = allocate_matrix(rows, cols);
transpose = allocate_matrix(rows, cols);
cout << "Enter matrix elements: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
transpose[j][i] = matrix[i][j] ;
}
}
cout << "Given matrix: " << endl;
display_matrix(matrix, rows, cols);
cout << endl;
cout << "Transpose: " << endl;
display_matrix(transpose, rows, cols);
dealloc_matrix(matrix, rows);
dealloc_matrix(transpose, rows);
return 0;
}