MULTIDIMENSIONAL ARRAYS
MULTI-DIMENSIONAL LISTS
 Two-dimensional lists are often used to represent tables of
values consisting of information arranged in rows and
columns.
 Can define one list for multiple sets of data
 List of Lists
 Identify a particular table element with two indices.
 By convention, the first identifies the element’s row and the second its
column.
 Multidimensional arrays can have more than two dimensions.
 Code: declaratory is number of rows by number of columns:
list[ROWS][cols]
 First declarator is number of rows; second is number of columns
TWO-DIMENSIONAL LIST REPRESENTATION
exams[4][3]
 Use two subscripts to access element:
exams[2][2] = 86
exams[0][0] exams[0][1] exams[0][2]
exams[1][0] exams[1][1] exams[1][2]
exams[2][0] exams[2][1] exams[2][2]
exams[3][0] exams[3][1] exams[3][2]
columns
r
o
w
s
EXAMPLE TWO-DIMENSIONAL LIST
TWO-DIMENSIONAL LIST INITIALIZATION
 Multidimensional listss can be initialized with list
initializers in declarations.
 A two-dimensional list b with two rows and two columns
could be declared and initialized with nested list
initializers as follows:
 exams= [ [84, 78],
[92, 97] ]
 Rows can have different lengths.
84 78
92 97
DECLARING MULTIDIMENSIONAL LISTS
TWO-DIMENSIONAL ARRAY AS PARAMETER,
ARGUMENT
 Use array name as argument in function call:
getExams(exams, 2)
 Use empty [] for row, size declarator for column in
prototype, header:
const int COLS = 2;
// Prototype
void getExams(int [][COLS], int);
// Header
void getExams(int exams[][COLS], int rows)
SUMMING ALL THE ELEMENTS IN A TWO-DIMENSIONAL LIST
 Question: How could we sum all the elements in a 2D list?

Multi-Dimensional Lists

  • 1.
  • 2.
    MULTI-DIMENSIONAL LISTS  Two-dimensionallists are often used to represent tables of values consisting of information arranged in rows and columns.  Can define one list for multiple sets of data  List of Lists  Identify a particular table element with two indices.  By convention, the first identifies the element’s row and the second its column.  Multidimensional arrays can have more than two dimensions.  Code: declaratory is number of rows by number of columns: list[ROWS][cols]  First declarator is number of rows; second is number of columns
  • 3.
    TWO-DIMENSIONAL LIST REPRESENTATION exams[4][3] Use two subscripts to access element: exams[2][2] = 86 exams[0][0] exams[0][1] exams[0][2] exams[1][0] exams[1][1] exams[1][2] exams[2][0] exams[2][1] exams[2][2] exams[3][0] exams[3][1] exams[3][2] columns r o w s
  • 4.
  • 5.
    TWO-DIMENSIONAL LIST INITIALIZATION Multidimensional listss can be initialized with list initializers in declarations.  A two-dimensional list b with two rows and two columns could be declared and initialized with nested list initializers as follows:  exams= [ [84, 78], [92, 97] ]  Rows can have different lengths. 84 78 92 97
  • 6.
  • 7.
    TWO-DIMENSIONAL ARRAY ASPARAMETER, ARGUMENT  Use array name as argument in function call: getExams(exams, 2)  Use empty [] for row, size declarator for column in prototype, header: const int COLS = 2; // Prototype void getExams(int [][COLS], int); // Header void getExams(int exams[][COLS], int rows)
  • 8.
    SUMMING ALL THEELEMENTS IN A TWO-DIMENSIONAL LIST  Question: How could we sum all the elements in a 2D list?