C++ introductory tutorial — 14 two-dimensional arrays

A two-dimensional array is an extension of a one-dimensional array.

For example, the one-dimensional array int a[5]={1,2,3,4,5}; Int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};

You can see that there are three arrays inside of three arrays. They all start at zero and go up to n minus one

#include <iostream>
using namespace std;

int a[3] [3] = {{1.2.3},
			{4.5.6},
			{7.8.9}};


int main (a)
{
  int i,j; // Subscripts start at zero
  for(i=0; i<3; i++) {for(j=0; j<3; j++) { cout<<a[i][j]<<"";
	}
	cout<<endl;
  	
  } 
  
  return 0;
}
Copy the code

Running results:



A typical multidimensional array would have several layers of loops. Once you get the logic straight, using arrays isn’t too hard.