0% found this document useful (0 votes)
79 views22 pages

Lecture 1.2.1

This document provides an overview of arrays as a data structure. It defines arrays as collections of similar data elements stored in contiguous memory locations that can be randomly accessed via indexes. The document outlines properties of arrays like fixed size and homogeneous elements. It also describes linear arrays, multi-dimensional arrays, sparse matrices, and different array representations in memory including row-major and column-major ordering.

Uploaded by

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

Lecture 1.2.1

This document provides an overview of arrays as a data structure. It defines arrays as collections of similar data elements stored in contiguous memory locations that can be randomly accessed via indexes. The document outlines properties of arrays like fixed size and homogeneous elements. It also describes linear arrays, multi-dimensional arrays, sparse matrices, and different array representations in memory including row-major and column-major ordering.

Uploaded by

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

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES 21CSH-211

DISCOVER . LEARN . EMPOWER


Contents
• Introduction
• Linear Array
• Linear Array Representations in Memory
• Multidimensional Array
• 2-D Array
• Representations in Memory
Array in Data Structure

• Arrays are defined as the collection of similar types of data items


stored at contiguous memory locations. It is one of the simplest data
structures where each data element can be randomly accessed by
using its index number.
• In C programming, they are the derived data types that can store the
primitive type of data such as int, char, double, float, etc. For
example, if we want to store the marks of a student in 6 subjects,
then we don't need to define a different variable for the marks in
different subjects. Instead, we can define an array that can store the
marks in each subject at the contiguous memory locations.
• Array is a container which can hold a fix number of items and these
items should be of the same type. Most of the data structures make
use of arrays to implement their algorithms. Following are the
important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical
index, which is used to identify the element.
Properties of array

• There are some of the properties of an array that are listed as


follows -
• Each element in an array is of the same data type and carries the
same size that is 4 bytes.
• Elements in the array are stored at contiguous memory locations
from which the first element is stored at the smallest memory
location.
• Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the size of the data element.
Arrays
• Arrays: - Linear DS🡪 Homogeneous (same type) data elements.
Index   Data
     
1   100
2   200
3   300
4   400
5   500
6   600
7   700
Arrays

• Easy to traverse
• Easy to search & sort
• Used to store relatively permanent collections of data.

• Types of arrays:-
• Linear arrays
• Two dimensional arrays
• Multidimensional arrays
Representation of Linear array
• LOC(LA[K]) = address of the element LA[K] of the array LA

• LOC(LA[K])=Base(LA)+w(K-lower bound)

• where w is the number of words per memory cell for the array
LA.

6/27/2020
Representation of Array in a Memory
The process to determine the address in a memory:
a) First address – base address.
b) Relative address to base address through index function.
Example: char X[100];
Let char uses 1 location storage.If the base address is 1200 then the next
element is in 1201.Index Function is written as:
Loc (X[i]) = Loc(X[0]) + i , i is subscript and LB = 0

1200 1201 1202 1203…………..

X[0] X[1] X[2]


Representation of Array in a Memory
▪ In general, index function:
Loc (X[i]) = Loc(X[LB]) + w*(i-LB);

where w is length of memory location required.


For real number: 4 byte, integer: 2 byte and character: 1 byte.
▪ Example:
If LB = 5, Loc(X[LB]) = 1200, and w = 4, find Loc(X[8]) ?

Loc(X[8])= Loc(X[5]) + 4*(8 – 5)


= 1212
Advantages:

1. It is used to represent multiple data items of same type by using only

single name.

2. It can be used to implement other data structures like linked lists,

stacks, queues, trees, graphs etc.

3. 2D arrays are used to represent matrices.


Disadvantages
1. We must know in advance that how many elements are to be stored
in array.
2. Array is static structure. It means that array is of fixed size. The
memory which is allocated to array can not be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we
allocate less memory than requirement, then it will create problem.

4. The elements of array are stored in consecutive memory locations.


So insertions and deletions are very difficult and time consuming.
Two-Dimensional Arrays

• A two-dimensional m ᵡ n array A is a collection of m.n data elements


such that each element is specified by a pair of integers, called
subscripts, with the property that
1 ≤ J ≤ m& 1≤K≤n

It is also called matrices/matrix arrays in mathematics, tables in


business applications.
Example
• Suppose each student in a class of 25 students is given 4 tests. Assuming
the students are numbered from 1 to 25, the test scores can be assigned to
a 25 ᵡ 4 matrix array SCORE as in table. Thus SCORE[K,L] contains the
Kth student’s score on the Lth test. In particular, the second row of the
array, Contains the four test scores of the second student.
SCORE[2,1], SCORE[2,2], SCORE[2,3], SCORE[2,4]

Student Test 1 Test 2 Test 3 Test 4


1 84 73 78 88
2 56 58 78 67
3 56 77 45 67
: : : : :
25 76 78 80 45

6/27/2020
Formula to calculate the Two-D Arrays

• Length = UB – LB +1

If integer NUM (2:5, -3:1), then find the length of 1st & 2nd dimensions.
Length of 1st dimension = 5 – 2 + 1 = 4
Length of 1st dimension = 1 – (-3) + 1 = 5
Number contains 4 * 5 = 20 elements.
Representation of 2-D Arrays

Divided into two parts: -


• Column-major order (Column by Column)
• Row-major order (Row by Row)
Computing of 2-D Arrays
• Column-major order:-
• LOC(A[J,K]) = Base(A) + w[M(K – 1) + (J – 1)]
• Row-major order: -
• LOC(A[J,K]) = Base(A) + w[N(J – 1) + (K – 1)]
eg:- Consider the 25 * 4 matrix array SCORE in previous example.
Suppose Base(SCORE) = 200 & there are w = 4 words per memory cell.
Furthermore, suppose the PL stores 2-D arrays using row-major order. Then the
address of SCORE[12,3], the third test of the 12th student, follows:
LOC(SCORE[12,3]) = 200 + 4 [4(12 – 1) + (3 – 1)]
= 200 + 4[46] = 384
Multidimensional Arrays

• Li = UB – LB + 1

for a given subscript Ki, the effective index Ei of Li is the number of


indices preceding Ki in the index set, & Ei can be calculated from
Ei = Ki – LB Base (C) + w[(…((E1L2 + E2)L3 + E3)L4 +….+EN-1 )LN
+ EN
How to Calculate Multidimensional Arrays
• Suppose a 3-D array MAZE is declared using MAZE(2:8, -4:1, 6:10). Then the lengths of the 3-D of
the MAZE are, respectively,
L1 = 8 – 2 + 1 = 7, L2 = 1 – (-4) + 1 = 6, L3 = 10 – 6 + 1 = 5
Accordingly, MAZE contains L1 . L2 . L3 = 7 * 6 * 5 = 210 elements
suppose the PL stores MAZE in memory in row-major order & suppose Base(MAZE) = 200 & there are
w = 4 words per memory cell.
eg: - The address of MAZE[5, -1, 8]:-
E1 = 5 – 2 = 3 E2 = -1 – (-4) = 3 E3 = 8 – 6 = 2
E1 L2 = 3 * 6 = 18
E1 L2 + E2 = 18 + 6 = 21
(E1 L2 + E2)L3 = 21 * 5 = 105
(E1 L2 + E2)L3 + E3 = 105 + 2 = 107
Therefore, LOC(MAZE[5, -1, 8] = 200 + 4(107) = 628
Sparse matrix
Matrix with relatively a high proportion of zero entries are called sparse
matrix. Two general types of n-square sparse matrices are there which
occur in various applications are mention in figure below(It is
sometimes customary to omit blocks of zeros in a matrix as shown in
figure below)
Triangular and Tridiagonal matrix
• Triangular matrix:
• This is the matrix where all the entries above the main diagonal
are zero or equivalently where non-zero entries can only occur
on or below the main diagonal is called a (lower)
• Tridiagonal matrix:
• This is the matrix where non-zero entries can only occur on the
diagonal or on elements immediately above or below the diagonal
is called a Tridiagonal matrix.
• The natural method of representing matrices in memory as two-
dimensional arrays may not be suitable for sparse matrices i.e. one
may save space by storing only those entries which may be non-
zero.
REFERENCES

• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley
Student Edition.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecturehtml

6/27/2020

You might also like