C++ Tutorial/Array/multi dimension array
Содержание
- 1 Creating A Multidimensional Array
- 2 Declare a two-dimension array
- 3 Fill a two dimensional integer array element by element
- 4 how to define, pass, and walk through the different dimensions of an array
- 5 Initialize a two-dimension array
- 6 Initializing multidimensional arrays
- 7 Search a two-dimension array
- 8 Use nested for loop to display the two dimesional array
- 9 Using pointer notation with a multidimensional array
Creating A Multidimensional Array
<source lang="cpp">#include <iostream>
int main() { int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}}; for (int i = 0; i<5; i++) for (int j=0; j<2; j++) { std::cout << "a[" << i << "][" << j << "]: "; std::cout << a[i][j]<< std::endl; } return 0; }</source>
a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
Declare a two-dimension array
<source lang="cpp">#include <iostream> using namespace std;
int main() {
int t,i, nums[3][4]; for(t=0; t < 3; ++t) { for(i=0; i < 4; ++i) { nums[t][i] = (t*4)+i+1; cout << nums[t][i] << " "; } cout << "\n"; } return 0;
}</source>
1 2 3 4 5 6 7 8 9 10 11 12
Fill a two dimensional integer array element by element
<source lang="cpp">#include <iostream> using namespace std; int main() {
int monthlytemps[4][7]; cout << "Enter the temp for week 1 day 1"; cin >> monthlytemps[0][0]; cout << "Enter the temp for week 1 day 2"; cin >> monthlytemps[0][1]; cout << "Enter the temp for week 1 day 3"; cin >> monthlytemps[0][2]; cout << "Enter the temp for week 1 day 4"; cin >> monthlytemps[0][3]; cout << "Enter the temp for week 1 day 5"; cin >> monthlytemps[0][4]; cout << "Enter the temp for week 1 day 6"; cin >> monthlytemps[0][5]; cout << "Enter the temp for week 1 day 7"; cin >> monthlytemps[0][6]; cout << "Enter the temp for week 2 day 1"; cin >> monthlytemps[1][0]; cout << "Enter the temp for week 2 day 2"; cin >> monthlytemps[1][1]; cout << "Enter the temp for week 2 day 3"; cin >> monthlytemps[1][2]; cout << "Enter the temp for week 2 day 4"; cin >> monthlytemps[1][3]; cout << "Enter the temp for week 2 day 5"; cin >> monthlytemps[1][4]; cout << "Enter the temp for week 2 day 6"; cin >> monthlytemps[1][5]; cout << "Enter the temp for week 2 day 7"; cin >> monthlytemps[1][6]; cout << "Enter the temp for week 3 day 1"; cin >> monthlytemps[3][0]; cout << "Enter the temp for week 3 day 2"; cin >> monthlytemps[3][1]; cout << "Enter the temp for week 3 day 3"; cin >> monthlytemps[3][2]; cout << "Enter the temp for week 3 day 4"; cin >> monthlytemps[3][3]; cout << "Enter the temp for week 3 day 5"; cin >> monthlytemps[3][4]; cout << "Enter the temp for week 3 day 6"; cin >> monthlytemps[3][5]; cout << "Enter the temp for week 3 day 7"; cin >> monthlytemps[3][6]; cout << "Enter the temp for week 4 day 1"; cin >> monthlytemps[4][0]; cout << "Enter the temp for week 4 day 2"; cin >> monthlytemps[4][1]; cout << "Enter the temp for week 4 day 3"; cin >> monthlytemps[4][2]; cout << "Enter the temp for week 4 day 4"; cin >> monthlytemps[4][3]; cout << "Enter the temp for week 4 day 5"; cin >> monthlytemps[4][4]; cout << "Enter the temp for week 4 day 6"; cin >> monthlytemps[4][5]; cout << "Enter the temp for week 4 day 7"; cin >> monthlytemps[4][6]; return 0;
}</source>
how to define, pass, and walk through the different dimensions of an array
<source lang="cpp">#include <iostream> using namespace std; void vdisplay_results(char carray[][3][4]);
char cglobal_cube[5][4][5]= { { {"1","1","1","1","1"}, {"2","2","2","2"," "}, {"3","3","3","3"," "}, {"4","4","4"," "," "}, }, { {"1","1","1","1","1"}, {"2","2","2","2"," "}, {"3","3","3","3"," "} }, { {"1","1","1","1","1"}, {"2","2","2","2"," "} }, { {"1","1","1","1","1"}, {"2","2","2","2"," "}, {"3","3","3","3"," "}, {"4","4","4"," "," "}, }, { {"1","1","1","1","1"}, {"2","2","2","2"," "}, {"3","3","3","3"," "}, {"4","4","4"," "," "}, }
}; int imatrix[4][3]={ {1},{2},{3},{4} };
int main( ) {
int irow_index, icolumn_index; char clocal_cube[2][3][4]; cout << "sizeof clocal_cube = "<< sizeof(clocal_cube) << "\n"; cout << "sizeof clocal_cube[0] = "<< sizeof(clocal_cube[0]) << "\n"; cout << "sizeof clocal_cube[0][0] = "<< sizeof(clocal_cube[0][0]) << "\n"; cout << "sizeof clocal_cube[0][0][0]= "<< sizeof(clocal_cube[0][0][0]) << "\n"; vdisplay_results(clocal_cube); cout << "cglobal_cube[0][1][2] is = " << cglobal_cube[0][1][2] << "\n"; cout << "cglobal_cube[1][0][2] is = " << cglobal_cube[1][0][2] << "\n"; for(irow_index=0; irow_index < 4; irow_index++) { for(icolumn_index=0; icolumn_index < 5; icolumn_index++) cout << cglobal_cube[0][irow_index][icolumn_index]; cout << "\n"; }
for(irow_index=0; irow_index < 4; irow_index++) { for(icolumn_index=0; icolumn_index < 5; icolumn_index++) cout << cglobal_cube[4][irow_index][icolumn_index]; cout << "\n"; } cout << "\nprint all of imatrix\n"; for(irow_index=0; irow_index < 4; irow_index++) { for(icolumn_index=0; icolumn_index < 3; icolumn_index++) cout << imatrix[irow_index][icolumn_index]; cout << "\n"; } return (0);
}
void vdisplay_results(char carray[][3][4]) {
cout << "sizeof carray =" << sizeof(carray) << "\n"; cout << " sizeof carray[0] =" << sizeof(carray[0]) << "\n"; cout << " sizeof cglobal_cube =" << sizeof(cglobal_cube) << "\n"; cout << " sizeof cglobal_cube[0] =" << sizeof(cglobal_cube[0]) << "\n";
}</source>
Initialize a two-dimension array
<source lang="cpp">#include <iostream> using namespace std;
int main()
{
int i, j; int sqrs[10][2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; cout << "Enter a number between 1 and 10: "; cin >> i; // look up i for(j=0; j<10; j++) if(sqrs[j][0]==i) break; cout << "The square of " << i << " is "; cout << sqrs[j][1]; return 0;
}</source>
Enter a number between 1 and 10: 7 The square of 7 is 49
Initializing multidimensional arrays
<source lang="cpp">#include <iostream> using std::cout; using std::endl; void display( const int [][ 3 ] ); int main() {
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; cout << "Values in array1 by row are:" << endl; display( array1 ); cout << "\nValues in array2 by row are:" << endl; display( array2 ); cout << "\nValues in array3 by row are:" << endl; display( array3 ); return 0;
} void display( const int a[][ 3 ] ) {
for ( int i = 0; i < 2; i++ ) { for ( int j = 0; j < 3; j++ ){ cout << a[ i ][ j ] << " "; } cout << endl; }
}</source>
Values in array1 by row are: 1 2 3 4 5 6 Values in array2 by row are: 1 2 3 4 5 0 Values in array3 by row are: 1 2 0 4 0 0
Search a two-dimension array
<source lang="cpp">#include <iostream>
- include <cstdio>
using namespace std;
int main() {
int i; char str[80]; char numbers[10][80] = { "A", "322", "B", "976", "C", "037", "D", "400", "E", "873" }; cout << "Enter name (A): "; cin >> str; for(i=0; i < 10; i += 2) if(!strcmp(str, numbers[i])) { cout << "Number is " << numbers[i+1] << "\n"; break; } if(i == 10) cout << "Not found.\n"; return 0;
}</source>
Enter name (A): 1 Not found.
Use nested for loop to display the two dimesional array
<source lang="cpp">#include <iostream.h> main() {
const int Length=4; int *p = new int[Length*Length]; int a[Length][Length]={{16, 2, 3,13},{ 5,11,10, 8},{ 9, 7, 6,12},{ 4,14,15, 1},}; for (int i = 0;i < Length; i++) for (int j = 0;j < Length; j++) p[ i * Length + j ] = a[ i ][ j ]; cout<<"display:\n"; for (int i = 0;i < Length; i++) { for (int j = 0;j < Length; j++) cout << p[ i * Length + j] <<" " ; cout << endl; } delete []p; return 0;
}</source>
display: 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
Using pointer notation with a multidimensional array
<source lang="cpp">#include <iostream>
- include <iomanip>
- include <cctype>
using std::cout; using std::endl; using std::setw; int main() {
const int table = 12; long values[table][table] = {0}; for(int i = 0; i < table ; i++) for(int j = 0; j < table ; j++) *(*(values + i) + j) = 0;
for(int i = 0 ; i < table ; i++) { for(int j = 0 ; j < table ; j++) cout << " " << setw(3) << values[i][j] << " |"; cout << endl; } return 0;
}</source>
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 0 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 0 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 0 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 0 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 0 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 0 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | 99 | 0 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 0 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | 110 | 121 |