You are on page 1of 5

OOP & Data Structures

CSE2123
(Lecture 05)

Dr. Naveed Bin Rais


naveedbinrais@yahoo.com

Copyright 2014-15, Naveed Bin Rais


(M.A.J.U.)

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

Name of an array can also be used as the address of its


zeroth element
int array[100];
int *ptr1 = &array[0];
int *ptr2 = array;
ptr1 and ptr2 are equal in this example
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

Arrays
Array name can also be treated as a pointer
Elements may be accessed by adding a particular value to the
array name

An array may be passed as a parameter to a function


Passed by reference, not by value
Optional to pass size of the array in the function

Difference between array of pointers & pointer to an


array
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

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

Functions can be written to concatenate two strings


Alternate is strcat(), strncat() functions

Function can be written to copy part of a string into another


Alternate is substr() function
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

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)

three int type arrays of 5 elements each

(0,1)
(1,1)
(2,1)

(0,2)
(1,2)
(2,2)

(0,3)
(1,3)
(2,3)

(0,4)
(1,4)
(2,4)

Can be used to implement matrices


Can be identified by rows and columns

Example: Students IDs and number of courses


Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

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

Two-dimensional array is of the format


A(0:e1, 0:e2)
If x is the base address of array A
Address of A(i,0) will be x + [i(e2 + 1) * (size of one element)]
Address of A(i,j) will be x + [(i(e2 + 1) + j) * (size of one element)]
Each row contains e2 + 1 elements

Another implementation could be an array of pointers


int* array[n];
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

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];

A 3-D Array could be of the format A(0:e1, 0:e2, 0:e3)


Address of A(i,0,0) is x+i(e2+1)(e3+1)
Address of A(i,j,0) is x+i(e2+1)(e3+1) + j(e3+1)
Address of A(i,j,k) is x+i(e2+1)(e3+1) + j(e3+1) + k
Where x is the base addresses of the array
Copyright 2014-15, Naveed Bin Rais
(M.A.J.U.)

You might also like