Multi-dimensional array implementation

Source: Internet
Author: User
Once an array is defined, its dimension and dimension fields will not change. Therefore, in addition to structure initialization and destruction, arrays only have access elements and modify elements. Arrays are generally divided into row order and column description. Generally, the system uses a line. Take a 2-dimensional array a [m] [N] as an example. Column order: A [0] [0] a [1] [0] ...... A [M-1] [0] ...... A [M-1] [0] A M-1] [1] ...... A [M-1] [n-1] row sequence: A [0] [0] a [1] [0] ...... A [M-1] [0] ...... A [0] [n-1] a [1] [[n-1] ...... A [M-1] [n-1]

Data is generally stored in a continuous address. The calculation method is (I, j) = & A + (B) + J; // the number of one-dimensional elements of B is extended to the multi-dimensional array (J1, J2, j3 ,....., JN) = (B2 * B3 *... * bn * J1 + B3 * B4 *... * bn * J2 + ..... + BN * JN-1 + JN) for example: known array a [5] [4] [10]. Find the address of a [2] [3] [4]; J1 = 2, J2 = 3, J3 = 4; b1 = 5, b2 = 4, B3 = 10; A [2] [3] [4] = b2 * B3 * J1 + B3 * J2 + J3 = 4*10*2 + 10*3 + 4; The following is the code implementation. (The following code passes a simple test in Ubuntu. If it cannot be run in another system, please forgive me. This code is only tested in a simple way. If any problem occurs, please forgive me .)
/** Created by reage at 2013 March 28 * Description: array implementation, including creation, assignment, access, and printing. ** blog: http://blog.csdn.net/rentiansheng#/#include <stdio. h> # include <stdlib. h> # include <stdarg. h> # define maxdim 4 typedef struct Array {int dim; int * PTR; int * bounds; int * base_add;} array; int array_init (array *, int dim ,...); int array_set_value (array * a, int value ,...); int array_print_line (array * A); int array_get_value (array * A ,...); void array_destory (array * A); int main (INT argc, char * argv []) {array a; int I = 0; Int J; int Total = 1; array_init (& A, 2, 4, 6); For (; I <4; I ++) {for (j = 0; j <6; j ++) {array_set_value (& A, total ++, I, j) ;}} array_print_line (& A); for (I = 0; I <4; I ++) {for (j = 0; j <6; j ++) {printf ("%-7D", array_get_value (& A, I, j ));} printf ("\ n");} array_destory (& A);} int array_init (array *, Int dim ,...) {if (1> dim | 8 <dim) Return-1; A-> dim = dim; va_list AP; int I; long Total = 1; a-> bounds = (int *) malloc (dim * sizeof (INT); va_start (AP, dim); for (I = 0; I <dim; I ++) {A-> bounds [I] = va_arg (AP, INT); Total * = A-> bounds [I];} va_end (AP ); a-> PTR = (int *) malloc (total * sizeof (INT); a-> base_add = (int *) malloc (dim * sizeof (INT )); a-> base_add [dim-1] = 1; I = dim-2; for (; I> = 0; I --) {A-> base_add [I] = A-> base_add [I + 1] * A-> bounds [I + 1];} return 0 ;} # define free (x) if (null! = (X) Free (x) void array_destory (array * A) {free (a-> PTR); free (a-> bounds ); free (a-> base_add);} int array_get_value (array * ,...) {va_list Va; va_start (va, a); int result = array_get_locate (A, VA); If (-1 = Result) Return-1; return a-> PTR [Result];} int array_print_line (array * A) {int Total = 1; int I = 0; int line; (; I <A-> dim; I ++) {total * = A-> bounds [I];} line = total/a-> bounds [0]; for (I = 0; I <total; I ++) {If (0 = I % line & 0! = I) printf ("\ n"); printf ("%-7D", a-> PTR [I]);} printf ("\ n "); return 0;} int array_get_locate (array * a, va_list VA) {int result = 0; int bound; int I; for (I = 0; I <A-> dim; I ++) {bound = va_arg (va, INT); If (0> bound | Bound> A-> bounds [I]) {return-1 ;} result + = bound * A-> base_add [I];} return result;} int array_set_value (array * a, int value ,...) {If (null = A) Return-1; va_list Va; va_start (va, value); int result = array_get_locate (A, VA); If (-1 = Result) return-1; A-> PTR [Result] = value; return 0 ;}

Blog:
Http://blog.csdn.net/rentiansheng/article/details/8737225

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.