forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_transpose.cpp
122 lines (104 loc) · 3.17 KB
/
matrix_transpose.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Matrix is a 2-D array.
The transpose of a matrix is simply a flipped version of the original matrix.
Transpose of a matrix can be computed by switching its rows with its columns.
This code gives output as transpose of input matrix.
*/
#include<iostream>
using namespace std ;
//Function that computes transpose.
void transpose(int frows, int fcolumns, int **finput_matrix, int **ftranspose_matrix)
{
int i = 0, j = 0 ;
for(i=0 ; i<fcolumns ; ++i)
{
for(j=0 ; j<frows ; ++j)
{
//According to definition of transpose, each element of transpose matrix is assigned.
ftranspose_matrix[i][j] = finput_matrix[j][i] ;
}
}
}
int main(void)
{
int **input_matrix = NULL, **transpose_matrix = NULL ;
int rows = 0, columns = 0, i = 0, j = 0 ;
//Taking input as number of rows and columns.
cout << "Enter the number of rows in matrix : " << endl ;
cin >> rows ;
cout << "Enter the number of columns in matrix : " << endl ;
cin >> columns ;
//Assigning the memory to the 2_D array according to input matrix size.
input_matrix = new int*[rows] ;
for(i=0 ; i<rows ; ++i)
{
input_matrix[i] = new int[columns] ;
}
cout << "Enter the elements of matrix in row wise manner separated by space : " << endl ;
//Taking user input as each element of matrix.
for(i=0 ; i<rows ; ++i)
{
for(j=0 ; j<columns ; ++j)
{
cin >> input_matrix[i][j] ;
}
}
//Assigning the memory to the transpose matrix. It will have reversed rows and columns as that of input matrix.
transpose_matrix = new int*[columns] ;
for(i=0 ; i<columns ; ++i)
{
transpose_matrix[i] = new int[rows] ;
}
//Call to the function that computes the transpose.
transpose(rows , columns , input_matrix , transpose_matrix) ;
//Display input matrix on the screen.
cout << "The input matrix is : " << endl ;
for(i=0 ; i<rows ; ++i)
{
for(j=0 ; j<columns ; ++j)
{
cout << input_matrix[i][j] << " " ;
}
cout << endl ;
}
cout << endl ;
//Display the transpose of input matrix on the screen.
cout << "The transpose matrix of above matrix is : " << endl ;
for(i=0 ; i<columns ; ++i)
{
for(j=0 ; j<rows ; ++j)
{
cout << transpose_matrix[i][j] << " " ;
}
cout << endl ;
}
//Deallocate the memory assigned to input matrix as well as transpose of it.
for(i=0 ; i<rows ; ++i)
{
delete[] input_matrix[i] ;
}
delete[] input_matrix ;
for(i=0 ; i<columns ; ++i)
{
delete[] transpose_matrix[i] ;
}
delete[] transpose_matrix ;
return 0 ;
}
/*
Sample I/O :
I/P :
2
3
1 2 3 4 5 6
O/P :
1 4
2 5
3 6
Explanation: Here, rows=2 & columns=3. On next line, each element of matrix is entered in rowwise manner separated by space.
So input matrix is: 1 2 3
4 5 6
The code gives output as its transpose.
Time Complexity : O(mn) , where m = number of rows & n = number of columns
Space Complexity : Transpose matrix requires extra storage same as input matrix. So space complexity becomes O(mn).
*/