Engineering Science 1036A/B Lecture 17: Arrays: Multidimensional Arrays
Multidimensional arrays with 2 dimensions:
aka two dimensional or 2-D arrays
ā
Represent tables of values with rows and columns
ā
Elements referenced with two subscripts ([ x ][ y ])
ā
An array with m rows and n columns is called an m-by-n array
ā
ā¢
Multidimensional arrays can have more than two dimensions
ā¢
Two-dimensional Arrays
General form:
dataType arrayName[rowSize][columnSize];
ā
int my2DArray[5][5];
Ā§
ā
ā¢
Initializer-list
int myArray[2][3]={{2, 5, 15}, {6, 21, 9}};
First row (row 0) contains 2, 5 and 15 for its three columns (column 0, 1
and 2)
ā
Second row (row 1) contains 6, 21 and 9 for its three columns.
ā
ā¢
int myArray[2][3]={{2, 5},{6, 21, 9}};
First row contains 2, 5 and 0 for its three columns
ā
Second row contains 6, 21 and 9 for its three columns
ā
ā¢
int myArray[2][3]={2, 5, 6, 21};
First row contains first three values 2, 5 and 6 for its three columns
ā
Second row contains second three values 21, 0 and 0 for its three
columns.
ā
Does not result in any compilation error but should be avoided
ā
ā¢
ā¢
Assigning Random Values to 2-D Array elements
Printing 2-D Array Values
Creating a function with a 2-D Array Parameter
Array parameter has to have the column size written inside the square brackets
Mandatory
ā
ā¢
Using the row size inside the square brackets is optionalā¢
void array2D(int matrix[][3], int rSize, int cSize);
ā¢
Multidimensional Arrays
Multidimensional arrays with 2 dimensions:
aka two dimensional or 2-D arrays
ā
Represent tables of values with rows and columns
ā
Elements referenced with two subscripts ([ x ][ y ])
ā
An array with m rows and n columns is called an m-by-n array
ā
ā¢
Multidimensional arrays can have more than two dimensions ā¢
Two-dimensional Arrays
General form:
dataType arrayName[rowSize][columnSize];
ā
int my2DArray[5][5];
Ā§
ā
ā¢
Initializer-list
int myArray[2][3]={{2, 5, 15}, {6, 21, 9}};
First row (row 0) contains 2, 5 and 15 for its three columns (column 0, 1
and 2)
ā
Second row (row 1) contains 6, 21 and 9 for its three columns.
ā
ā¢
int myArray[2][3]={{2, 5},{6, 21, 9}};
First row contains 2, 5 and 0 for its three columns
ā
Second row contains 6, 21 and 9 for its three columns
ā
ā¢
int myArray[2][3]={2, 5, 6, 21};
First row contains first three values 2, 5 and 6 for its three columns
ā
Second row contains second three values 21, 0 and 0 for its three
columns.
ā
Does not result in any compilation error but should be avoided
ā
ā¢
ā¢
Assigning Random Values to 2-D Array elements
Printing 2-D Array Values
Creating a function with a 2-D Array Parameter
Array parameter has to have the column size written inside the square brackets
Mandatory
ā
ā¢
Using the row size inside the square brackets is optionalā¢
void array2D(int matrix[][3], int rSize, int cSize);
ā¢
Multidimensional Arrays
Multidimensional arrays with 2 dimensions:
aka two dimensional or 2-D arrays
ā
Represent tables of values with rows and columns
ā
Elements referenced with two subscripts ([ x ][ y ])
ā
An array with m rows and n columns is called an m-by-n array
ā
ā¢
Multidimensional arrays can have more than two dimensions ā¢
Two-dimensional Arrays
General form:
dataType arrayName[rowSize][columnSize];
ā
int my2DArray[5][5];
Ā§
ā
ā¢
Initializer-list
int myArray[2][3]={{2, 5, 15}, {6, 21, 9}};
First row (row 0) contains 2, 5 and 15 for its three columns (column 0, 1
and 2)
ā
Second row (row 1) contains 6, 21 and 9 for its three columns.
ā
ā¢
int myArray[2][3]={{2, 5},{6, 21, 9}};
First row contains 2, 5 and 0 for its three columns
ā
Second row contains 6, 21 and 9 for its three columns
ā
ā¢
int myArray[2][3]={2, 5, 6, 21};
First row contains first three values 2, 5 and 6 for its three columns
ā
Second row contains second three values 21, 0 and 0 for its three
columns.
ā
Does not result in any compilation error but should be avoided
ā
ā¢
ā¢
Assigning Random Values to 2-D Array elements
Printing 2-D Array Values
Creating a function with a 2-D Array Parameter
Array parameter has to have the column size written inside the square brackets
Mandatory
ā
ā¢
Using the row size inside the square brackets is optionalā¢
void array2D(int matrix[][3], int rSize, int cSize);
ā¢
Multidimensional Arrays
Document Summary
Multidimensional arrays with 2 dimensions: aka two dimensional or 2-d arrays. Represent tables of values with rows and columns. Elements referenced with two subscripts ([ x ][ y ]) An array with m rows and n columns is called an m-by-n array. Multidimensional arrays can have more than two dimensions. Initializer-list int myarray[2][3]={{2, 5, 15}, {6, 21, 9}}; First row (row 0) contains 2, 5 and 15 for its three columns (column 0, 1 and 2) Second row (row 1) contains 6, 21 and 9 for its three columns. int myarray[2][3]={{2, 5},{6, 21, 9}}; First row contains 2, 5 and 0 for its three columns. Second row contains 6, 21 and 9 for its three columns. Second row contains 6, 21 and 9 for its three columns int myarray[2][3]={2, 5, 6, 21}; First row contains first three values 2, 5 and 6 for its three columns. Second row contains second three values 21, 0 and 0 for its three columns.