-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPARSE.C
51 lines (50 loc) · 1001 Bytes
/
SPARSE.C
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
//Program of Sparse matrix for 3-tuple method using array.
#include<conio.h>
#include<stdio.h>
#define srow 50
#define mrow 20
#define mcolumn 20
void main(){
int a[mrow][mcolumn],sparse[srow][3];
int i,j,mr,mc,sr,nz=0,s=1;
clrscr();
printf("\nEnter the size of the matrix:");
scanf("%d %d",&mr,&mc);
for(i=0;i<mr;i++){
for(j=0;j<mc;j++){
printf("\nEnter element for %d row, %d column",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix:\n");
for(i=0;i<mr;i++){
for(j=0;j<mc;j++){
printf("%d ",a[i][j]);
if(a[i][j]!=0)
nz++;
}
printf("\n");
}
sr = nz + 1;
sparse[0][0] = mr;
sparse[0][1] = mc;
sparse[0][2] = nz;
for(i=0;i<mr;i++){
for(j=0;j<mc;j++){
if(a[i][j]!=0){
sparse[s][0] = i+1;
sparse[s][1] = j+1;
sparse[s][2] = a[i][j];
s++;
}
}
}
printf("\nSparse Matrix:\n");
for(i=0;i<sr;i++){
for(j=0;j<3;j++){
printf("%d ",sparse[i][j]);
}
printf("\n");
}
getch();
}