-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathsparseMatrix.c
64 lines (47 loc) · 1.16 KB
/
sparseMatrix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
int nonZero;
void sparseMatrix(int arr[100][100],int rows,int columns){
int crr[rows][nonZero],p=0,q=0;
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
if (arr[i][j]>0){
crr[p][q]=i;
crr[p+1][q]=j;
crr[p+2][q]=arr[i][j];
q++;
}
}
}
for(int f=0; f<rows;f++){
for(int g=0;g<nonZero;g++){
printf("%d ", crr[f][g]);
}
printf("\n");
}
}
int main()
{
int rows,columns,arr[100][100],flag=0;
printf("Enter rows of matrix: ");
scanf("%d",&rows);
printf("\nEnter columns of matrix: ");
scanf("%d",&columns);
printf("\nEnter elements of matrix:\n");
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
scanf("%d",&arr[i][j]);
if(arr[i][j]==0){
flag++;
}
}
}
nonZero = (rows*columns)- flag;
if (flag>=nonZero){
printf("\nSparse Matrix:\n");
sparseMatrix(arr,rows,columns);
}else{
printf("\nNot a sparse matrix");
}
return 0;
}