Most of the space in a two-dimensional array is not used. To increase the efficiency of array memory usage, We need to compress it.
Well, go to the code
1 # include <stdio. h>
2 # include <stdlib. h>
3
4 int main (INT argc, char * argv [])
5 {
6 int sparse [5] [10] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
7 0, 0, 0, 9, 0, 0, 0, 0, 0, 0,
8 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
9 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
10 0, 0, 0, 0, 0, 0, 0, 6, 0}; // Sparse Matrix
11 int compress [6] [3]; // compress the Array
12 int I, J, K;
13 k = 1;
14 compress [0] [0] = 5; // The array sparse has 5 rows.
15 compress [0] [1] = 10; // The array sparse has 10 columns
16 compress [0] [2] = 5; // The array sparse has 5 elements.
17 For (I = 0; I <5; I ++) // 2D array Traversal
18 {
19 For (j = 0; j <10; j ++)
20 {
21 if (sparse [I] [J]! = 0) // The element is not used
22 {
23 compress [k] [0] = I; // number of storage lines
24 compress [k] [1] = J; // number of storage Columns
25 compress [k] [2] = sparse [I] [J]; // store element values
26 K ++; // The next row
27}
28}
29}
30 For (I = 0; I <6; I ++) // compress the array output
31 {
32 For (j = 0; j <3; j ++)
33 {printf ("% 3d", compress [I] [J]);}
34 printf ("\ n ");
35}
36
37
38 system ("pause ");
39 return 0;
40}
41