This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtranspose_of_sparse_matrix.cpp
59 lines (51 loc) · 1.62 KB
/
transpose_of_sparse_matrix.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
// Transpose of a sparse matrix
#include<iostream>
#include<string>
using namespace std;
int main() {
int row,column,size;
// input for number of rows and columns and number of non zero elements
cout<<"Enter the number of rows in the matrix: ";
cin>>row;
cout<<"Enter the number of columns in the matrix: ";
cin>>column;
cout<<"Enter the number of non zero elements in the matrix: ";
cin>>size;
// inputing the matrix in sparse form (triplet form)
if(size <= (row*column)){
int sparseMatrix[size][3];
for(int i=0 ; i<size ; i++){
cout<<"Enter the row index , column index and value of element "<<i+1<<endl;
cin>>sparseMatrix[i][0];
cin>>sparseMatrix[i][1];
cin>>sparseMatrix[i][2];
}
// Displaying your matrix
cout<<"Your sparse matrix is :\n";
for(int i=0 ; i<size ; i++){
cout<<sparseMatrix[i][0]<<" "<<sparseMatrix[i][1]<<" "<<sparseMatrix[i][2]<<endl;
}
// Finding the transpose
int transpose[size][3];
int k=0;
for(int i=0 ; i<column ; i++){
for(int j=0 ; j<size ; j++){
if(sparseMatrix[j][1] == i){
transpose[k][0] = sparseMatrix[j][1];
transpose[k][1] = sparseMatrix[j][0];
transpose[k][2] = sparseMatrix[j][2];
k++;
}
}
}
// Displaying the transpose of the sparse matrix
cout<<"\n\nTranspose of sparse matrix is :\n";
for(int i=0 ; i<size ; i++){
cout<<transpose[i][0]<<" "<<transpose[i][1]<<" "<<transpose[i][2]<<endl;
}
}
else{
cout<<"Error\n";
}
return 0;
}