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] ...... M-1] [0]
.............
A [M-1] [0] a M-1] [1] ...... a [m-1] n-1]


Row sequence:
A [0] [0] a [1] [0] ...... 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; // number of one-dimensional elements of B
Extended to multi-dimensional arrays (j1, j2, j3 ,....., jn) = (b2 * b3 *... * bn * j1 + b3 * b4 *... * bn * j2 + ..... + bn * JN-1 + jn)
For example, an array a [5] [4] [10] is known. Obtain 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 .)
[Plain]
/*
* 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 * a, int dim ,...);
Int array_set_value (Array * a, int value ,...);
Int array_print_line (Array * );
Int array_get_value (Array * ,...);
Void array_destory (Array * );
 
Int main (int argc, char * argv [])
{
Array;
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 (& );
For (I = 0; I <4; I ++)
{
For (j = 0; j <6; j ++)
{
Printf ("%-7d", array_get_value (& a, I, j ));
}
Printf ("\ n ");
}
Array_destory (& );
}
 
Int array_init (Array * a, 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 *)
{
FREE (a-> ptr );
FREE (a-> bounds );
FREE (a-> base_add );
}
 
Int array_get_value (Array * ,...)
{
Va_list va;
Va_start (va, );

Int result = array_get_locate (a, va );
If (-1 = result) return-1;
Return a-> ptr [result];
}
 
 
Int array_print_line (Array *)
{
Int total = 1;
Int I = 0;
Int line;
For (; 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;
}

/*
* 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 * a, int dim ,...);
Int array_set_value (Array * a, int value ,...);
Int array_print_line (Array * );
Int array_get_value (Array * ,...);
Void array_destory (Array * );

Int main (int argc, char * argv [])
{
Array;
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 (& );
For (I = 0; I <4; I ++)
{
For (j = 0; j <6; j ++)
{
Printf ("%-7d", array_get_value (& a, I, j ));
}
Printf ("\ n ");
}
Array_destory (& );
}

Int array_init (Array * a, 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 *)
{
FREE (a-> ptr );
FREE (a-> bounds );
FREE (a-> base_add );
}

Int array_get_value (Array * ,...)
{
Va_list va;
Va_start (va, );

Int result = array_get_locate (a, va );
If (-1 = result) return-1;
Return a-> ptr [result];
}


Int array_print_line (Array *)
{
Int total = 1;
Int I = 0;
Int line;
For (; 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;
}

 

Related Article

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.