Professional Documents
Culture Documents
CSE2123
(Lecture 05)
Arrays: One-Dimensional
Finite ordered set of homogeneous elements
Finite
specific number of elements
Ordered
elements are arranged from zeroth to size-1
Homogeneous
all elements in the array are of same type
Example:
int students[100];
Two operations
Extraction
Accessing an element of an array at an index i
Storing
Writing at an index i of an array
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)
Arrays: One-Dimensional
Size of an array is specified at the time of declaration
Lower bound is specified as zeroth element
Upper bound is specified as size-1
Arrays
Array name can also be treated as a pointer
Elements may be accessed by adding a particular value to the
array name
Arrays: Strings
Strings can be thought as an array of characters
Each string must be terminated by a NULL character \0
Many operations can be performed over strings
Functions can be written to compute length of a string
Alternate is strlen(), strnlen() functions
Arrays: Strings
Useful string functions (defined in string.h)
strlen()
strcpy(),
strcat(),
strchr(),
strcmp(),
strstr()
strncpy()
strncat()
strrchr()
strncmp()
Exercise:
Write a function to determine whether a given string is a
palindrome or not
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)
Arrays: Two-Dimensional
Can also be termed as array of arrays
int array[3][5]
(0,0)
(1,0)
(2,0)
(0,1)
(1,1)
(2,1)
(0,2)
(1,2)
(2,2)
(0,3)
(1,3)
(2,3)
(0,4)
(1,4)
(2,4)
Arrays: Two-Dimensional
Difference between logical and physical view of data
Multi-dimension is only a logical view
Computer can only organize elements in memory as a set of linear
elements
Arrays: Multi-Dimensional
How to represent a three-dimensional array?
First subscript specifies a plane number
Second subscript is a row number
Third subscript is used for column number
Example:
An array to represent position of a ball in three coordinates space
int position[x][y][z];