0% found this document useful (0 votes)
42 views12 pages

Array: Prepared By: Abdul Jalil Niazai

An array is a collection of similar data types stored in contiguous memory locations. It allows accessing the elements using an index. There are two types of arrays: one-dimensional and two-dimensional. A one-dimensional array stores data in a linear fashion using a single index, while a two-dimensional array represents data in a table-like format using row and column indices. Elements in an array can be accessed using the dope vector method by calculating the memory address based on the starting address and index.

Uploaded by

baryalai ahmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views12 pages

Array: Prepared By: Abdul Jalil Niazai

An array is a collection of similar data types stored in contiguous memory locations. It allows accessing the elements using an index. There are two types of arrays: one-dimensional and two-dimensional. A one-dimensional array stores data in a linear fashion using a single index, while a two-dimensional array represents data in a table-like format using row and column indices. Elements in an array can be accessed using the dope vector method by calculating the memory address based on the starting address and index.

Uploaded by

baryalai ahmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Array

 Array is the collection of finite same type of data,


which are stored in successive memory locations.
 Often more useful to think of an array as a
collection of variables of the same type.
 The data item of the array are called the elements
of array and each data item is stored in separate
memory location.
 Each memory location within the array is identifies
by its position value, which is called the index of the
array.

Prepared By: Abdul Jalil Niazai


Example Of Array
Memory

110 112 114 116 118


Location
22 11 13 52 21

X[1] X[2] X[3] X[4] X[5]

Element
Index

Prepared By: Abdul Jalil Niazai


Types Of Array
There are two types of array:
1) One dimensional array
2) Two dimensional array

 One Dimensional Array


One dimensional array represents the data in the form of vector, list, or
linear form. It is used to store similar type of data consisting of only one
row and column.

110 112 114 116 118


Location
22 11 13 52 21

X[0] X[1] X[2] X[3] X[4]

Prepared By: Abdul Jalil Niazai


Accessing the elements of array
from computer memory
• The elements of array can be accessed using Dope
Vector Method.
• Dope Vector Method
This method is used to access the elements of array by using
the proper formula, which accesses the memory address of the
required element.
Formula : MA(i)=BA + w ( i - 1 )
where
MA = Required Memory Address i = Index
BA = Starting Memory Address w = word size / Length

Prepared By: Abdul Jalil Niazai


Example…
• Consider the following array
114 116 118
110 112
22 11 13 52 21

X[1] X[2] X[3] X[4] X[5]


Now, Suppose we have to find the memory address of x[4], then

MA(i) = BA + w ( i – 1)
MA(4) = 110 + 2 (4 – 1)
= 110 + 6
= 116
So the required memory address of x[4] is 116 and the required
element is 52.

Prepared By: Abdul Jalil Niazai


Creation of 1-D Array
• Use only keyword new to create a one-
dimensional array.
type [ ] variable_name '=' 'new‘ type
[integer_expression ] ;
• Specifies variable_name as the name of the one-
dimensional array variable.
• Specifies type as each element's type.
• Specifies keyword new, followed by type, followed
by integer_expression between square brackets ([ ]), which
identifies the number of elements.
• Specifies = to assign the one-dimensional array's reference
to variable_name.

Prepared By: Abdul Jalil Niazai


Example…
• The following code fragment use only
keyword new to create a one-dimensional array that
stores data items based on a primitive type.
int [] test_scores = new int [4];

• int [] test_scores declares a one-dimensional array variable


(test_scores) along with that variable's type (int []).
• The int [] reference type signifies that each element must
contain a data item of primitive type integer.
• new int [4] creates a one-dimensional array by allocating
memory for four consecutive integer elements.
• Each element holds a single integer and initializes to 0.
• Array initializer : int [] test_scores = new int [] { 70, 80, 20,
30 }; Prepared By: Abdul Jalil Niazai
Two Dimensional Array
• A finite set of Elements in which items can be stored
in rows as well as in columns
• Size or Length = m * n
• Two-dimensional array also known as matrix or table.
• Two-dimensional (2D) arrays are indexed by two
subscripts, one for the row and one for the column.
COLUMNS
A[1,1] A[1,2] A[1,3]
ROWS A[2,1] A[2,2] A[2,3]
A[3,1] A[3,2] A[3,3]

Prepared By: Abdul Jalil Niazai


Accessing of 2D Array Element
• Using Dope Vector method the element of 2D can be accessed in two ways.

1) Accessing 2-D array in Row Major Order


To understand the concept of row major order, consider the following
Matrix 2 X 3
12 9 16
5 11 12
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 12
X(1,1) X(1,2) X(1,3) X(2,1) X(2,2) X(2,3)

Prepared By: Abdul Jalil Niazai


Continuous…
• Suppose we have to access x(2,1) element memory location, then we
use the following formula.
MA(i,j) = BA + w [ n ( i – 1 ) + ( j – 1 )]
where
“ i “ represent row and “ j “ represent column and “n” are the total
number of columns.
now
MA(2,1) = 110 + 2 [ 3 (2-1) + (1 – 1)]
= 110 + 6
= 116 which is required memory address.

Prepared By: Abdul Jalil Niazai


Continuous…
2.)Accessing 2-D array in Column Major Order
To understand the concept of column major order, consider
the following matrix 2 X 3
12 9 16
5 11 22
Now the storage of above matrix in column order is

110 112 114 116 118 120


12 5 9 11 16 22
X(1,1) X(2,1) X(1,2) X(2,2) X(1,3) X(2,3)

Prepared By: Abdul Jalil Niazai


Continuous…
• Suppose we have to access X(1,3) element memory location, then
we use the formula.

MA(i,j) = BA + w[m ( j – 1) + ( i – 1)]


where ‘m’ is the total number of rows.
Now
MA(1,3) = 110 + 2[ 2 (3-1) + (1-1)]
=110 + 8
=118
Which is the required memory location.

Prepared By: Abdul Jalil Niazai

You might also like