-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroshan sparse matrix.cpp
More file actions
59 lines (55 loc) · 954 Bytes
/
roshan sparse matrix.cpp
File metadata and controls
59 lines (55 loc) · 954 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# include<stdio.h>
int main(){
int row,col,k=0;
printf("Enter the no of Rows and Columns: \n");
scanf("%d%d",&row,&col);
int a[row][col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("Enter the element at %d , %d",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The entered array is: \n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
int z=0,nz=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(a[i][j]==0){
z++;
}
else{
nz++;
}
}
}
int b[nz][3];
if(nz>z){
printf("Not a sparse matrix");
return 0;
}
else{
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(a[i][j]!=0){
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}
}
}
}
printf("\n");
for(int i=0;i<nz;i++){
for(int j=0;j<3;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
}