-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_to_sparse.cpp
More file actions
38 lines (35 loc) · 818 Bytes
/
Copy pathmatrix_to_sparse.cpp
File metadata and controls
38 lines (35 loc) · 818 Bytes
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
#include <iostream>
#include<vector>
using namespace std;
typedef struct
{
int row=0;
int col=0;
int value=0;
}term;
int main()
{
int matrix[100][100];
term sparse[10];
int n, m, i, j, k=0;
cout<<"Enter the number of rows and columns of matrix: ";
cin>>n>>m;
cout<<"\nEnter the matrix: ";
for(i=0; i<n;i++){
for(j=0; j<m; j++){
cin>>matrix[i][j];
if(matrix[i][j]!=0){
sparse[k].col= j;
sparse[k].row = i;
sparse[k].value = matrix[i][j];
k++;
}
}
}
cout<<"The Sparse matrix is:"<<endl;
cout<<"Row:\tColumn:\tValue:\n";
for(i=0; i<k; i++){
cout<<sparse[i].row<<"\t\t"<<sparse[i].col<<"\t\t"<<sparse[i].value<<endl;
}
return 0;
}