Ch.9 Arrays

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 DocumentRight 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 DocumentRight 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 DocumentRight 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
How Arrays are Stored in Memory An array is a group of consecutive memory location. All the elements of an array have the same name and data type. Example1: (One-dimensional array) int a[6]; One dimensional array a has six elements. The data type of array elements is int. Array a occupies 24 consecutive bytes in the memory as shown on the right. The elements of an array can be referred by the format arrayname[position_number] First element at position 0. In our example, the elements of array a can be represented as a[0], a[1], a[2], a[3], a[4], and a[5].
Background image of page 7

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

View Full DocumentRight Arrow Icon
Example2: (Two-dimensional array) int b[2][3]; Two dimensional array b has six elements (2*3) with 2 horizontal rows and 3 vertical columns. From a programming point of view, it is a block of memory with each row of the
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 87

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

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online