-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDS2.C
More file actions
69 lines (66 loc) · 1.48 KB
/
Copy pathDS2.C
File metadata and controls
69 lines (66 loc) · 1.48 KB
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
//data structure program 2
// Sparse Array
#include<stdio.h>
#include<conio.h>
void main()
{
system("cls");
int a[10][10], b[10][3],p,q,r;
int i,j;
int z;
printf("Enter Number of rows and columns \n");
scanf("%d %d", &p,&q);
printf("Enter Elements \n");
z=0;
for(i=0; i<=p-1; i++)
for(j=0; j<=q-1; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]!=0)
z++;
}
if(z>(p*q)/2)
{
printf("Array is not Sparse \n");
getch();
}
else
{
printf("\n Entered Array is = \n");
for(i=0; i<=p-1; i++)
{
for(j=0; j<=q-1; j++)
{
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
b[0][0] =p;
b[0][1]=q;
b[0][2]=z;
r=1;
for(i=0; i<=p-1; i++)
for(j=0; j<=q-1; j++)
{
if(a[i][j]!=0)
{
b[r][0]=i+1;
b[r][1]=j+1;
b[r][2]=a[i][j];
r++;
}
}
printf("Triplet form of given sparse array= \n");
for(i=0; i<r; i++)
{
for(j=0; j<3; j++)
{
printf(" %d ", b[i][j]);
printf(" ");
}
printf("\n");
}
getch();
}
}