Two-dimensional array

Definition

, for example:

float a [3] [4], b [5] [10];

Definition A is an array of 3 * 4 (3 lines 4 columns), B is an array of 5 * 10 (5 lines 10 columns). Note that

float a [3, 4], b [5, 10];

is not easy to understand, a C language program and its operation results:

 #include int main (int Argc, const char * argv []) {int Array [3] [5] = {0}; // Define a two-dimensional array (3 rows 5 columns) int Temp = 0 ; // Set a temporary integer variable to assign a value for the array for (int A = 0; a 

operation result is (for easy viewing, finishing into a table):

array [0] [0] = 1

array [0] [1] = 2 < / p>

array [0] [2] = 3

array [0] [3] = 4

array [0] [4] = 5

array [1] [0] = 6

array [1] [1] = 7

Array [1] [2] = 8

array [1] [3] = 9

array [1] [4] = 10

array [2] [1] = 12

array [2] [2] = 13

array [2] [3] = 14 < / p>

array [2] [4] = 15

overview

Two-dimensional array a [m] [n], this is a M line, N-column two-dimensional array. Set a [P] [q] for a first element of A, that is, two-dimensional array The subscript from P to M + P, column subscript from q to n + q, press "Row priority order" storage When it is stored, the address of element a [i] [j] is calculated as:

LOC (a [i] [ J]) = LOC (a [p] [q]) + (i - p) * n + (j - q)) * t

When storing by "column priority order", the address is calculated To:

LOC (a [i] [j]) = LOC (a [p] [q]) + ((j - q) * m + (i - p)) * t

Store at least the number of cells required (M-P + 1) * (N-Q + 1) * T by-byte

Basic operation

Transposition matrix

// wherein A, B is M * N matrix:

Two-dimensional array

 void track (Matrix A, Matrix B) {INT I, J; for (i = 0; I 

matrix phase Add

 //) A, B, C is M * N matrix: Void AddMat (Matrix C, Matrix A, Matrix B) {INT I, J; for (i = 0; I 

matrix multiplied

 // wherein A is M * n matrix, B is N * 1 matrix, C is M * 1 matrix Void Mutmat (Matrix C, Matrix A, Matrix B ) {INT I, J, K; For (i = 0; i 

related concept

C ++ dynamic 2D array:

to shaping For example, the ROW is the number of rows, and the COL is the number of numbers

int ** data; // stores a 2D array pointer (pointing to the pointer to the pointer. Date = x [0] [0] of the address. Such a standard is better. Because Sizeof (Date) result is 4 不 不 二 二 数 数 组)

 // The following implementation How to apply for a memory Data = new int * [rot]; for (int K = 0; k 
 // Assignment, for example, DATA [0] [0] = 5; // = 5 = 5 = 5 (called 0 row 0 columns in C ++) to 5 // Delete Memory for (int) i = 0; I 

See

matrix

  • matrix

  • sparse matrix

Related Articles
TOP