-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg11.c
73 lines (60 loc) · 1.74 KB
/
Prog11.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
65
66
67
68
69
70
71
72
73
/* ---------------PROBLEM STATEMENT ---------------
Implement Sparse Array.
*/
#include<stdio.h>
int main(){
int row , col;
printf("Enter the Numbers of the rows : ");
scanf(" %d",&row);
printf("Enter the Number of the columns : ");
scanf(" %d",&col);
int sparse[row][col];
int totalElements = row * col;
int nonzeroElements = 0;
printf("Enter the data in the sparse matrix\n");
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
scanf("%d",&sparse[i][j]);
if(sparse[i][j] != 0){
nonzeroElements++;
}
}
}
printf("\n********* Sparse Matrix ****************\n");
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
printf("%d ",sparse[i][j]);
}
printf("\n");
}
if(nonzeroElements > totalElements/4){
printf("\n***********ERROR*********\n");
printf("\nGiven matrix is NOT sparse matrix!!\n");
}
else{
int tripletRow = nonzeroElements+1;
int triplet[tripletRow][3];
triplet[0][0] = row;
triplet[0][1] = col;
triplet[0][2] = nonzeroElements;
int k = 1;
for(int i = 0 ; i< row; i++){
for(int j = 0 ; j< col; j++){
if(sparse[i][j] != 0){
triplet[k][0] = i;
triplet[k][1] = j;
triplet[k][2] = sparse[i][j];
k++;
}
}
}
printf("\n********* Triplet Matrix ****************\n");
for(int i = 0 ; i< tripletRow ; i++){
for(int j = 0 ; j<3;j++){
printf("%d ", triplet[i][j]);
}
printf("\n");
}
}
return 0;
}