You are on page 1of 12

Array

Muhammad Irfan
What is an array?
 Array is the collection of similar data items and is
also called a linear data structure.
 The data type of all elements of array is the same
and each element of array is referred by one name.
 All array elements are stored at contiguous memory
locations and each location have a subscript to
distinguish each element from the other and just
because of this reason array is also sometimes called
subscripted variable.
 There are two types of array
 One dimensional array
 Two dimensional array
One dimensional array
 One dimensional array is a finite number of
elements and elements can be stored in one
dimensional array in either row or in column.
A
10 20 30 40 50 60 70

A[0] A[1] A[2] A[3] A[4] A[5] A[6]


 The size of one dimensional array can be
calculated by the following formula
size = Ub – Lb +1
Accessing one dimensional
array by Dope Vector method.
 When an array is stored in memory then the following formula
is used for accessing one dimensional array using dope vector
method.
MA(i) = Sa+(i-1)*w
or
loc[Array(i)]=ba+i-1
Where
MA is the memory address of the element
Sa= Start address
i= subscript of an element to be accessed
w=word length
( for integer w=2, for float w=4 and for character w=1)
Two Dimensional Array
 The collection of similar data items in which all the elements are
stored in the form of rows as well as columns is called two
Dimension array.
 If m is the number of rows and n is the number of columns
then the formula for the size of two Dimension is as under
size = m×n.
 If there are three students and got marks in five different
subjects then it can be stored in the form of two dimension
array as under:
sub1 sub2 sub3 sub4 sub5
s1 68 83 49 74 55
s2 76 66 37 59 88
s3 44 60 53 21 64
Con’t
 This array having the tabular form as under:
result
sub1 sub2 sub3 sub4 sub5
s1 68 83 49 74 55
s2 76 66 37 59 88
s3 44 60 53 21 64

Suppose the array name is “result” and if we want to access the


2nd student 3rd subject marks then the statement will be
result(2,3)
While 2 shows row number and 3 shows column number
Accessing two dimension array
 We know that the computer memory is
the combination of adjacent location
and it can’t store the information in the
form of table.
 So the information should be store
either using row by row mapping
technique or column by column
mapping technique
Row-by-Row mapping
 In this technique the elements of the two dimension
array will store row-wise.
 When the elements of the first row finished then the
2nd row is started and so on.
 In row by row mapping technique the number of row
is constant while the number of column changes for
each element of a particular row.
e.g 34 21 11
56 83 32
12 43 05
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)

Result 34 21 11 56 83 32 12 43 05
Con’t
 The following Dope vector formula is used to
access a particular element of three
dimension array.
loc[A(i,j)] = ba+n(i-1)+(j-1)
where
ba = base address or starting address
n = Total number of columns
i = Row number of a particular element
j = Column number of element
Column-by-Column mapping
That type of mapping in which all the elements
of an array stores column wise that is first
elements of 1st column stores then 2nd column
and so on, is called column by column
mapping
e.g 34 21 11
56 83 32
12 43 05
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)
34 56 12 21 83 43 11 32 05
Con’t
Formula for accessing a particular element using
dope vector using column-by-column mapping
is as under
loc[A(i,j)] = ba+m(j-1)+(i-1)
where
A = array name
ba= base address or starting address
m = total number of rows
i = particular row number
j = particular column number
Drawback of Dope Vector Method
 The main drawback of this method is
the wastage of time.
 As accessing a particular item in the
array involves calculation which will
waste some time.
 To remove this drawback we will use
the ILIFFE method.

You might also like