{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ch.9 Arrays - Arrays Outline Declarations of Arrays How...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays Outline Declarations of Arrays How Arrays are Stored in Memory Initialization Pass Arrays of Fixed Length to Function Pass One-Dimensional Arrays to Function Pass Two-Dimensional Arrays to Function Variable Length Arrays (VLA) Pass VLA to Functions Deferred-Shape Arrays Sample Problem Vector and Matrix Operations Extensions in Ch Assume-Shape Arrays Arrays with Explicit Lower and Upper Bounds
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Declaration of Arrays One dimensional array Using following format to declare a one dimensional array arrayType arrayName[expr]; where expr is an integral expression. The value of expr is the number of elements of the array. For example, int a[6]; declares a one-dimensional array. The name of array is a . The data type of array elements is int. Array a has 6 elements from a [0] to a [5].
Background image of page 2
How array elements are referenced int i, a[6]; a[0] = 10; a[5] = 2*a[0]; i = a[0]+a[5]
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Two-dimensional array Using following format to declare a two-dimensional array arrayType arrayName[expr1][expr2]; expr1 and expr2 are integral expressions. The value of expr1 specifies the number of rows of the array and the value of expr2 specifies the number of columns of the array. For example, int a[2][3]; declares a two-dimensional array. Array a has 2 rows and 3 columns with 6 elements.
Background image of page 4
Definitions Related to Arrays: The number of dimensions in an array is referred to as the rank of the array. The number of elements in each dimension is called the extent in the corresponding dimension of the array. The shape of an array is a vector where each element of the vector is the extent in the corresponding dimension of the array. The size of an array is the number of bytes used to store the total number of elements of the array. For example, for array a below int a[3][4]; The rank of array a is 2, the extents corresponding to the first and second dimensions are 3 and 4, respectively. The shape of the array is a vector with two elements 3 and 4 as [3, 4], the size of the array is sizeof(int)*3*4 = 4*3*4=48 bytes.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Size of Array The sizeof operator can be used to determine the size of an array. > int a[6] > sizeof(a) 24 > int c[3][4] > sizeof(c) 48 > double d[3][4] > sizeof(d) 96 > char c[6] > sizeof(c) 6
Background image of page 6