Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.

Similar presentations


Presentation on theme: "Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element."— Presentation transcript:

1 Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element. These are referred to as multidimensional arrays. An array that requires two index values to reference an element is called a two-dimensional array. double carrots[3][4]; To reference a particular element of the carrots array, we need two index values: the first index value specifies the row (0-2), and the second index value specifies a particular column (0-3).

2 Multidimensional Arrays tMyn2 The arrangement of this array in memory is shown below: carrots[0][0]carrots[0][1]carrots[0][2]carrots[0][3] carrots[1][0]carrots[1][1]carrots[1][2]carrots[1][3] carrots[2][0]carrots[2][1]carrots[2][2]carrots[2][3] This row is carrots[2] This row is carrots[0] This row is carrots[1]

3 Multidimensional Arrays tMyn3 The rows are stored contiguously in memory. So the two-dimensional array is effectively a one- dimensional array of three elements, each of which is a one-dimensional array with four elements. To display the entire array, one row to a line, we must write something like:

4 Multidimensional Arrays tMyn4 #include "stdafx.h" #include using namespace System; using namespace std; int main(array ^args) { double carrots[3][4]={ {2.5, 3.2, 3.7, 4.1}, {4.1, 3.9, 1.6, 3.5}, {2.8, 2.3, 0.9, 1.1} };

5 Multidimensional Arrays tMyn5 for (int i=0; i<3; i++) { for(int j=0; j<4; j++) cout<<setw(5)<<carrots[i][j]; cout<<endl; } return 0; }

6 Multidimensional Arrays tMyn6 If there are not enough to initialize all the elements in the row, then the elements without values will be initialized to 0: double carrots[3][4]={ {2.5, 3.2}, {4.1}, {2.8, 2.3, 0.9} }; The elements will therefore be initialized as follows:

7 Multidimensional Arrays tMyn7 carrots[0][0] 2.5 carrots[0][1] 3.2 carrots[0][2] 0.0 carrots[0][3] 0.0 carrots[1][0] 4.1 carrots[1][1] 0.0 carrots[1][2] 0.0 carrots[1][3] 0.0 carrots[2][0] 2.8 carrots[2][1] 2.3 carrots[2][2] 0.9 carrots[2][3] 0.0 It is possible to zero all the elements in the array with the statement: double carrots[3][4]={0};

8 Multidimensional Arrays tMyn8 If we include several initial values in the initializer list, but omit the nested braces enclosing values for the rows, values are assigned sequentially to the elements, as they are stored in memory: double carrots[3][4]={1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7}; The array will be set up with the values shown below:

9 Multidimensional Arrays tMyn9 carrots[0][0] 1.1 carrots[0][1] 1.2 carrots[0][2] 1.3 carrots[0][3] 1.4 carrots[1][0] 1.5 carrots[1][1] 1.6 carrots[1][2] 1.7 carrots[1][3] 0.0 carrots[2][0] 0.0 carrots[2][1] 0.0 carrots[2][2] 0.0 carrots[2][3] 0.0

10 Multidimensional Arrays tMyn10 It is possible to let the compiler determine the size of the first (leftmost) dimension of any array from the set of initializing values: double carrots[][4]={ {2.5, 3.2}, {4.1}, {2.8, 2.3, 0.9} };

11 Multidimensional Arrays tMyn11 If we initialize a two-dimensional array of type char with character strings between double quotes, we don’t need the braces around the string for a row: char cars[][30]={ “VW Jetta”, “Toyota Avensis”, “Volvo v40”, “Audi A4” }; A terminating null character ‘\0’ will be appended to each string.


Download ppt "Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element."

Similar presentations


Ads by Google