Data structure C language> array> compressed two-dimensional array

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.