Page 84 - PowerPoint Presentation
P. 84
CAVITE STATE UNIVERSITY
T3 CAMPUS
Department of Information Technology DCIT 25 – Data Structures and Algorithms
dimensional array where there are 3 elements in the first dimension and 4 elements in the
second dimension.
The element grades[0] is said to “point” to the second dimension of the array, which
is referenced as grades[0][0]. The second dimension is considered an array. Therefore,
programmers say that the first element of a multidimensional array points to another array
(that is the second dimension)
The data type tells the computer that each element of the array will contain an integer
data type. The data type is followed by the array name and two values that indicate the size
of each dimension used for the array. In this case, there are three sets of four array elements.
You declare a multidimensional array and initializes its elements by using French
braces, as shown in the figure below. There are three sets of inner French braces. Each of
these represents the first dimension of the array. There are also four values within each set of
inner French braces. These values are assigned to each element of the second dimension of
the array.
Assigning Values to a Multidimensional Array
You assign a value to an element of a multidimensional array with an assignment
statement similar to the assignment statement that assigns a value to a single dimensional
array, as shown here.
grades[0][0] = 1001
You must specify the index for both dimensions. In this example, the integer 1001 is
assigned to the first element of the first set of elements in the grades array.
Referencing the Contents of a Multidimensional Array
The contents of elements of a multidimensional array can be used in a program by
referencing the index of both dimensions of the array element. The figure below shows you
how to display the final exam grade for the second student.
QUIZ 4. Question and Answer (3 pts each)
1. What is the difference between an array element and a variable?
2. How do you assign a value to an element of an array?
3. How to declare single and multidimensional array?
Page | 31