A detailed explanation of the one-dimensional array and two-dimensional array _c language in C + +

Source: Internet
Author: User
Tags first row

C + + one-dimensional array
Defining a one-dimensional array

The general format for defining a one-dimensional array is:
Type identifier array name [constant expression];
For example:

  int a[10];


It represents an array named A, which is an integral type and has 10 elements.

A few notes on one-dimensional arrays:
1 the array name naming rules and variable names are the same, followed by identifier naming rules.

2 A constant expression enclosed in square brackets denotes the subscript value, as the following wording is valid:

  int a[10];
  int a[2*5];
  int a[n*2]; Suppose n is defined as a constant variable

3 The value of the constant expression represents the number of elements, that is, the length of the array. For example, in "int a[10];" , 10 indicates that a array has 10 elements, the subscript starting from 0, and the 10 elements are: a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7], a[8],a[9]. Note that the last element is a[9] rather than a[10].

4 constant expressions can include constants, constant variables, and symbolic constants, but cannot contain variables. In other words, C + + does not allow the size of the array to be dynamically defined, that is, the size of the arrays does not depend on the value of the variable during the program's operation. For example, the following definition of an array is not possible:

  int n;
  cin>>n; Enter the length of an array
  int a[n];//attempt to determine the length of an array based on the value of n


It is legal to change line 1th and 2 to the following line:

  const int n=5;


Referencing elements of a one-dimensional array

The array must be defined first and then used. You can only reference the value of an array element individually and not all of the elements in the entire array at once.

The representation of an array element is:
Array name [subscript]
The subscript can be an integer constant or an integral type expression. For example:

  a[0]= a[5]+ a[7]-A[2*3]

A reference to an array element of the example.

#include <iostream>
using namespace std;
int main ()
{
  int i, a[10];
  for (i=0;i<=9;i++)
   a[i]=i;
  for (i=9;i>=0;i--)
   cout<<a[i]<< "";
  cout<<endl;
  return 0;
}

The results of the operation are as follows:

9 8 7 6 5 4 3 2 1 0

The program causes the value of a[0]~a[9] to be 0~9 and then output in reverse order.
Initialization of one-dimensional arrays

1 assigns an initial value to the array elements when defining the arrays. For example:

  int a[10]={0,1,2,3,4,5,6,7,8,9};

2 can assign values to only a subset of the elements. For example:

  int a[10]={0,1,2,3,4};

3 If you want to make an array of all the elements in the value of 1, you can write:

  int a[10]={1,1,1,1,1,1,1,1,1,1};


Can't write

  int a[10]={1*10};


You cannot assign an initial value to an array as a whole.

4 When you assign an initial value to all array elements, you can not specify the length of the array. For example:

  int a[5]={1,2,3,4,5};


can be written

  int a[]={1,2,3,4,5};


example of one-dimensional array program

"Example" uses an array to deal with Fibonacci sequence problems.

You can use 20 elements to represent the 20 numbers in a series, starting with the 3rd number, you can use the expression f[i]=f[i-2]+f[i-1] to find each number. The procedure is as follows:

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
  int i;
  int f[20]={1,1}; F[0]=1,f[1]=1
  for (i=2;i<20;i++)
   f[i]=f[i-2]+f[i-1];//at 2 o'clock in I, f[2]=f[0]+f[1], and so on for
  (i=0;i <20;i++//The function of this loop is to output 20
  {
   if (i%5==0) cout<<endl;//control line wrap, output 5 data per line
   COUT<<SETW (8) < <f[i]; 8 column width per data output
  }
  cout<<endl;//Last Run line wrap return
  0;
}

The results of the operation are as follows:


"Example" to write a program, with the bubble method to 10 number of numbers sorted (in order from small to large).

Bubble method is the idea: the two adjacent numbers are compared, will be small to the ahead. See Figure 5.2. Then proceed to the 2nd trip comparison, the remaining 5 number of the preceding method for comparison, see figure.



It can be inferred that if there are n numbers, then the n-1 trip comparison (and Exchange). In the 1th trip to carry out n-1 22 comparison, in the J trip to the N-j times 22 comparison.

According to the above ideas to write procedures, this set n=10, this example defines the length of the array 11,a[0] do not use, only a[1]~a[10], in order to meet the habits of people. As you know from the previous narrative, you should make 9 comparisons and exchanges.

#include <iostream>
using namespace std;
int main ()
{
  int a[11];
  int I, j, T;
  cout<< "Input numbers:" <<endl;
  for (i=1;i<11;i++)//input a[1]~a[10]
   cin>>a[i];
  cout<<endl;
  for (j=1;j<=9;j++)//A total of 9 trip comparison for
   (i=1;i<=10-j;i++)//In each trip to (10-j) times 22 comparison
     if (a[i]>a[i+1))// If the previous number is greater than the following number
     {
      t=a[i];a[i]=a[i+1];a[i+1]=t;
     } Swap two number positions to make decimals float
  cout<< "The sorted numbers:" <<endl;
  for (i=1;i<11;i++)//output 10 number
   cout<<a[i]<< "";
  cout<<endl;
  return 0;
}

The operating conditions are as follows:

Input numbers:
3 5 9 11 33 6-9-76 100 123↙ The
sorted numbers:
-76-9 3 5 6 9 11 33 100

C + + two-dimensional array
An array with two subscripts is called a two-dimensional array. Some data depends on two factors to determine the only, for example, 3 students, each student has 4 course results, obviously, the performance data is a two-dimensional table, as shown in the Book table.


To show that the 3rd student's 4th course results, we need to point out the number of students and the number of courses in the two factors, in mathematics, s3,4 expressed. In C + + s[3][4], it represents data 73.
Defining two-dimensional arrays

The general form of defining two-dimensional arrays is:
Type identifier array name [constant expression] [constant expression];
For example:

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


A single-precision array that defines a as 3x4 (3 rows, 4 columns), and B is a single-precision array of 5x10 (5 rows, 10 columns). Note Can not be written as "float a[3,4], b[5,10];" C + + defines a two-dimensional array in such a way that we can think of a two-dimensional array as a special one-dimensional array: its element is a one-dimensional array. For example, you can think of a as a one-dimensional array that has 3 elements: a[0],a[1],a[2], and each element is a one-dimensional array of 4 elements, as shown in Figure 5.4. A[0],A[1],A[2] is the name of the 3 one-dimensional array.


The two-dimensional array defined above can be understood to define 3 one-dimensional arrays, which are equivalent to:

  Float a[0][4], a[1][4], a[2][4];


Here, A[0],a[1],a[2] is used as a one-dimensional array name. This approach to C + + is convenient for array initialization and pointer representation, which will be realized later.

In C + +, the order of elements in a two-dimensional array is stored in rows, in which the elements of the first row are stored sequentially in memory, and the elements of the second row are stored. The figure represents the order in which the a[3][4] array is stored.

The two-dimensional array defined above can be understood to define 3 one-dimensional arrays, which are equivalent to:

  Float a[0][4], a[1][4], a[2][4];


Here, A[0],a[1],a[2] is used as a one-dimensional array name. This approach to C + + is convenient for array initialization and pointer representation, which will be realized later.

In C + +, the order of elements in a two-dimensional array is stored in rows, in which the elements of the first row are stored sequentially in memory, and the elements of the second row are stored. Figure 5.5 shows the order in which the a[3][4] array is stored.


C + + allows multidimensional arrays to be used. With the basis of two-dimensional arrays, it is not difficult to master multidimensional arrays. For example, the method for defining a three-dimensional array is:
float a[2][3][4];
Defines a float-type three-dimensional array of a, which has 2x3x4=24 elements. Multidimensional array elements in memory order: the first dimension of the subscript change the slowest, the rightmost subscript change the fastest. For example, the elements in the three-dimensional array are arranged in the following order:

Copy Code code as follows:
A[0][0][0]→A[0][0][1]→A[0][0][2]→A[0][0][3]→A[0][1][0]→A[0][1][1]→A[0][1][2]→A[0][1][3]→A[0][2][0]→A[0][2][1] →A[0][2][2]→A[0][2][3]→A[1][0][0]→A[1][0][1]→A[1][0][2]→A[1][0][3]→A[1][1][0]→A[1][1][1]→A[1][1][2]→A[1][1][3] →A[1][2][0]→A[1][2][1]→A[1][2][2]→A[1][2][3]

A reference to a two-dimensional array

The representation of the elements of a two-dimensional array is:

 Array name [subscript] [subscript]


Such as:

  A[2][3]


The subscript can be an integer expression, such as a[2-1][2*2-1]. Do not write a[2,3],a[2-1,2*2-1] form.

An array element is a left value that can appear in an expression or be assigned a value, for example:

  B[1][2]=A[2][3]/2;


When using an array element, you should be aware that the subscript value should be within the scope of the defined array size. The error that often occurs is:

  int a[3][4]; Defines an array of 3 rows 4 columns
  ┆
  a[3][4]=15;//reference a[3][4] Element


Defines an array of 3x4, which has a maximum row subscript value of 2 and a maximum column coordinate value of 3. You can use up to a[2][3],a[3][4] to exceed the range of the array.

Make a strict distinction between the a[3][4] used when defining an array and the a[3][4 when referencing elements. The former a[3][4] is used to define the dimensions of the array and the size of each dimension, and the 3 and 4 of the latter a[3][4] are subscript values, a[3][4] represent an element.
Initialization of two-dimensional arrays

You can initialize a two-dimensional array in the following ways:
1) Branch to the two-dimensional array to assign initial value. Such as

   int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};


This method of assigning initial value is more intuitive, assigning the data in the 1th curly braces to the elements of Line 1th, and the data in the 2nd curly braces to the elements in the 2nd line ... The initial value is assigned by row.

2 You can write all the data in a curly brace, in order to arrange the array of the elements of the initial value. Such as

  int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};


The effect is the same as before. But the 1th approach is good, one line to one line, the boundaries clear. Use the 2nd method if the data is more, write a large, easy to omit, also difficult to check.

3 can assign an initial value to some elements. Such as:

  int a[3][4]={{1},{5},{9}};


Its role is to assign an initial value to only the elements in the 1th column of each row, and the remaining element values are automatically set to 0. After assigning an initial value, the elements of the array are:

1 0 0 0 5 0 0 0 9 0 0-
0


You can also assign an initial value to an element in each row:

  int a[3][4]={{1},{0,6},{0,0,11}};


The array elements that are initialized are as follows:

1 0 0 0 0 6 0 0 0 0 11-
0


This method is convenient for less than 0 elements, without having to write out all 0, just enter a small amount of data. You can also assign an initial value to only a few lines of elements:

  int a[3][4]={{1},{5,6}};


The array elements are:

1 0 0 0 5 6 0 0 0 0 0-
0


The 3rd line does not assign an initial value. You can also not assign an initial value to line 2nd:

  int a[3][4]={{1},{},{9}};

4 If all the elements are assigned an initial value (that is, all initial data is provided), the length of the first dimension may not be specified when the array is defined, but the length of the second dimension cannot be saved. Such as:

  int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};


can be written

  int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};


The system will allocate storage based on the total number of data, 12 data, 4 columns per row, of course, 3 lines can be determined.

You can also assign an initial value to only some elements and omit the length of the first dimension, but you should assign an initial value to the branch. Such as

  int a[][4]={{0,0,3},{},{0,10}};


This way, you can tell the compiler system: the array has 3 rows. The elements of the array are:

0 0 3 0 0 0 0 0 0 10 0-
0

C + + uses the two square brackets of a[][when defining arrays and representing array elements, which is useful when initializing an array, making the concept clear, easy to use, and error-prone.
Examples of two-dimensional array programs

The example swaps a two-dimensional array of row and column elements and saves them to another two-dimensional array. For example:


The procedure is as follows:

#include <iostream>
using namespace std;
int main ()
{
  int a[2][3]={{1,2,3},{4,5,6}};
  int b[3][2],i,j;
  cout<< "Array A:" <<endl;
  for (i=0;i<=1;i++)
  {for
   (j=0;j<=2;j++)
   {
     cout<<a[i][j]<< ";
     B[J][I]=A[I][J];
   }
   cout<<endl;
  }
  cout<< "Array B:" <<endl;
  for (i=0;i<=2;i++)
  {for
   (j=0;j<=1;j++)
     cout<<b[i][j]<< "";
   cout<<endl;
  }
  return 0;
}

The results of the operation are as follows:

Array A:
 1 2 3
 4 5 6
array B:
 1
 4 2 5 + 3

"Example" has a 3x4 matrix that requires the codec to find the value of the element with the largest value, along with the line number and column number.

At the beginning, assign the value of a[0][0] to the variable max, and then let the next element compare to it, save the value of the two to Max, and then the next element to the new Max ratio until the last element is finished. Max's last value is the maximum value in all elements of the array. The procedure is as follows:

#include <iostream>
using namespace std;
int main ()
{
  int i,j,row=0,colum=0,max;
  int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};
  max=a[0][0];//the value for Max to start with a[0][0] for
  (i=0;i<=2;i++)//From line No. 0 ~ 2nd for
   (j=0;j<=3;j++)/from No. 0 column to 3rd column
     if (a I [J]>max)//If an element is greater than Max
     {
      max=a[i][j],//max will take the value of the element
      row=i;/Note the line number of the element I
      colum=j;//Note the column number of the element J
     }
  cout<< "max=" <<max<< ", row=" <<row<< ", colum=" <<colum<<endl;
  return 0;
}

Output results are

Max=56,row=0,colum=3

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.