C + + Two-dimensional array interpretation, two-dimensional array declaration and initialization

Source: Internet
Author: User

we know that one-dimensional space is a line, and mathematics is expressed in a single axis; two-dimensional space is a plane, and mathematics is expressed in a planar coordinate system. What about a two-dimensional array?
lines and faceswe use a subscript to describe an element in a one-dimensional array, as if we were using a number to describe a point on a line. And all the data is stored on a single line. If we use two subscript, we can form a plane, like a table, with rows and columns, all the data can be stored in the table.
we refer to the two subscript of the two-dimensional array, respectively, as row subscript and column subscript, in the front row subscript, in the following column subscript.

So when do you want to use a two-dimensional array? Generally there are two situations, one is to describe a two-dimensional thing. For example, with 1 for the wall, 0 for the path, we can use a two-dimensional array to describe a maze map, 1 for a path, 0 for no access, we can use a two-dimensional array to describe the traffic situation between several cities. There is also a description of multiple things with multiple attributes. For example, there are many students, each student has Chinese, maths and English, we can use a two-dimensional array to describe.

for the second case, we should note that each attribute should be of the same data type, such as three disciplines where the scores are integers. If names (string attributes) appear, they cannot be combined into a two-dimensional array. Therefore, do not attempt to integrate attributes of different data types into a two-dimensional array.
Declaration and initialization of two-dimensional arraysThe declaration of a two-dimensional array is similar to a one-dimensional array, except that it has only one subscript:
data type array name [number of rows] [number of columns];

Note that the subscript for the two-dimensional array also starts at 0.

the initialization of two-dimensional arrays is divided into three kinds, one is sequential initialization, one is the initialization by the line, we can see a program, we will be able to understand them: (program 7.4.1)
#include "iostream.h"
#include "iomanip.h"
int main ()
{
int array1[3][2]={4,2,5,6};//sequence initialization
int array2[3][2]={{4,2},{5},{6}};//is initialized by row
cout << "array1" <<endl;
for (int i=0;i<3;i++)//output array array1
{
for (int j=0;j<2;j++)
{
cout <<SETW (2) <<array1[i][j];
}
cout <<endl;
}
cout << "Array2" <<endl;
for (int k=0;k<3;k++)//output array array2
{
for (int l=0;l<2;l++)
{
cout <<SETW (2) <<array2[k][l];
}
cout <<endl;
}
return 0;
}

Operation Result:
array1
4 2
5 6
4
array2
4 2
5 8
6 8

As we can see, the so-called sequential initialization is first initialized from left to right and from top to bottom, that is, all elements in the first row are initialized and then initialized to the second row. By row initialization, a pair of curly braces is used to represent each row, skipping elements that were not initialized in the previous line, and initializing from left to right in the row. For an element that is not initialized, it is an indeterminate value.
omit the size of the first dimensionwe learned in the first section that the size of a one-dimensional array can be omitted. However, the number of elements in a two-dimensional array is the product of the number of rows and columns, and if we only tell the number of computer elements, the computer can not know exactly how many rows of this array. Therefore, C + + stipulates that only the first dimension (number of rows) can be omitted when declaring and initializing a two-dimensional array. For example:
int array[][3]={1,2,3,4,5,6};
equivalent to:
int array[2][3]={1,2,3,4,5,6};
storage of two-dimensional arrays in memoryIt has been previously stated that memory relies on an address to determine the only storage unit in memory, that is, only one parameter. So in memory, all of the data is stored in order, like a one-dimensional array. So how does a two-dimensional array with two subscripts be stored in memory?

in memory, the first row of the two-dimensional array is stored sequentially, followed by the second row of data, then the third row of data ... The image on the right (Figure 7.4) shows a two-dimensional array of memory storage.
passing a two-dimensional array to a functionwe know that the array is passed as a parameter to the function, which is the address of the first element of the array. This is also true for two-dimensional arrays. But there are two problems, one is that we have to let the function know the number of rows and columns, as if we were to let the function know the size of a one-dimensional array to prevent cross-border access. The other thing is that we have to let the computer know what the two-dimensional array is. A table, that is, the number of columns of the array must be told. This is the same as the number of rows that can only be omitted from a two-dimensional array. Let's look at a program that passes a two-dimensional array to a function:
#include "iostream.h"
#include "iomanip.h"
void disp (int a[][2],int r,int c);//Tell the array the number of columns
int main ()
{
int array[3][2]={4,2,5,6,3,1};
cout << "Array" <<endl;
disp (array,3,2);
return 0;
}
void disp (int a[][2],int r,int c)
{
for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++)
{
cout <<SETW (2) <<a[i][j];
}
cout <<endl;
}
}

Operation Result:
Array
4 2
5 6
3 1
converting two-dimensional arrays into one-dimensional arrayssometimes we find it convenient to use a two-dimensional array to describe the same thing. For example, we use a two-dimensional array to draw a maze map, row subscript and column subscript just like a Cartesian coordinate system. In some cases, however, you cannot use a two-dimensional array, or it is difficult to create a two-dimensional array. Two-dimensional arrays are stored in memory in the same way as one-dimensional arrays, so we have to replace them with a one-dimensional array.

Thus, it is not difficult to summarize a result, a two-dimensional array element A[x][y] in a one-dimensional array B, is:
a[x][y]=b[x* number of columns +y]

C + + Two-dimensional array interpretation, two-dimensional array declaration and initialization

Related Keywords:

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.