SlideShare a Scribd company logo
Chapter-3
Linear Data Structure - Arrays
MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE
Reference: Data Structures with C by Seymour Lipschutz,
Schaum’s Outlines Series.
INTRODUCTION
• Data structures are classified as either linear or nonlinear.
• A data structure is said to be linear, if its elements form a sequence.
There are two basic ways of representing linear structures in memory.
1. Using arrays.
2. Using linked lists.
• LINEAR ARRAYS
A linear array is a list of a finite number N of homogeneous data elements (i.e.
data elements of the same type) such that:
a) The elements of the array are referred respectively by an index set consisting
of n consecutive numbers
b) The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array. If not explicitly
stated, we will assume the index set consists of the integers 1, 2, …. N.
In general, the length or the number of data elements of the array can be
obtained from the index set by the formula
Length=UB – LB + 1
Where UB is the largest index, called the upper bound, and LB is the smallest
index, called the lower bound of the array. (Length= UB when LB=1).
The elements of an array A may be denoted by the subscript notation
A1, A2, A3, ….. An
OR by the bracket notation A[1], A[2], A[3], ….. A[n]
The number K in A[K] is called a subscript or an index and A[K] is called
a subscripted variable.
Example
• Let DATA be a 6-element linear array of integers such that DATA[1]=247
DATA[2]=56 DATA[3]=429 DAT[4]=135 DATA[5]=87 DATA[6]=156.
• We will denote such an array by DATA: 247, 56, 429, 135, 87, 156
Problems:
1) Consider the linear array LA(1:10).
Find the number of elements in an array.
Ans: The number of elements is equal to the length; hence the
formula
Length= UB – LB + 1
Length(LA) = 10 – 1 + 1
= 10
2) Consider the linear array AA(5:50), BB(-5:10) and CC(18). Find
the number of elements in each array.
Ans:
Length= UB – LB + 1
a) Length(AA) = 50 – 5 + 1
= 46
b) Length(BB) = 10 – (-5) + 1
= 16
c) Length(CC) = 18 – 1 + 1
= 18
Abstract Data Types (ADT)
• An abstract data type refers to a set of data values and associated
operations that are specified accurately, independent of
implementation.
• With an ADT, it is possible to know what a data type does, but how it
does is hidden.
• An ADT can further be defined as a data declaration packaged
together with the operations that are meaningful for the data. User
may not have the knowledge of data structure.
ARRAY AS ADT
• An array is a fundamental Abstract Data Type. An array is pre-
defined set which has a sequence of cells where we can store
different types of elements.
• Consider the following example: object (A, N). Here an array A is
created which can store N number of items in it.
• A[i] is an item in the array stored in its ith position.
• REPRESENTATION OF LINEAR ARRAYS IN MEMORY
• Let LA be a linear array in the memory of the computer. The notation
LOC(LA[K])=address of the element LA[K] of the array LA.
• Let LA[10]={5, 6, 12, 4, 15, 45, 87, 1, 9, 13} and Base address be 100.
• The elements of linear array LA are stored in successive
memory locations.
• Computer does not need to keep track of the address of
every element of LA, but it needs the base address Base(LA).
• Using the address of Base(LA), the computer calculates the
address of any element of LA by the following formula:
• LOC(LA[K]) = Base(LA) + w(K – Lower Bound)
Where w is number of words per memory cell for the array LA
Problems:
1) Consider the linear array AA(1:10).
Suppose Base(AA)=100 and w=4 words per memory cell
for AA.
Find the address of LA[5].
Ans:
LOC(AA[K]) = Base(AA) + w(K-LB)
LOC(AA[5]) = 100 + 4 (5-1)
= 100 + 16
= 116
2) Consider the linear array AA(5:50).
Suppose Base(AA)=300 and w=4 words per memory cell for AA.
Find the address of a) AA[15], b) AA[35] and c) AA[55].
LOC(AA[K]) = Base(AA) + w(K-LB)
a) LOC(AA[15]) = 300 + 4 (15-5)
= 300 + 40
= 340
b) LOC(AA[35]) = 300 + 4 (35-5)
= 300 + 120
= 420
c) AA[55] is not an element of AA, since 55 exceeds UB=50.
TRAVERSING A LINEAR ARRAY
• Let LA be the collection of data elements stored in the memory of the
computer. Suppose we want to print the contents of each element of
LA or suppose we want to count the number of elements of LA with
each given property.
• This can be accomplished by traversing A, that is, by accessing and
processing each element of the array exactly once is called
traversing.
• Algorithm 4.1: (Traverse a Linear Array) Here LA is a Linear array with
lower boundary LB and upper boundary UB. This algorithm traverses
LA applying an operation Process to each element of LA.
1. [Initialize counter] Set K:=LB.
2. Repeat Steps 3 and 4 while K≤UB.
3. [Visit element] Apply PROCESS to LA[K].
4. [Increase counter] Set K:=K+1.
[End of Step 2 loop]
5. Exit.
The alternate algorithm for traversing (using for loop) is:
Algorithm 4.1: (Traverse a Linear Array) This algorithm traverse a linear
array LA with lower bound LB and upper bound UB.
1. Repeat for K=LB to UB
Apply PROCESS to LA[K].
[End of loop]
2. Exit.
Problems:
1) Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
Find the values of the given arrays after each loop.
a) Repeat for K = 1 to 4:
Set AB[K + 1] := A[K]
[End of loop]
Ans:
First AB[2] := A[1] sets AB[2]=25, the value of A[1]
Then AB[3] := A[2] sets AB[3]=32, the value of A[2]
Then AB[4] := A[3] sets AB[4]=65, the value of A[3]
Then AB[5] := A[4] sets AB[5]=43, the value of A[4]
Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
b) Repeat for K = 4 to 1 by -1:
Set A[K + 1] := A[K]
[End of loop]
Ans:
First A[5] := A[4] sets A[5]=78
Then A[4] := A[3] sets A[4]=43
Then A[3] := A[2] sets A[3]=65
Then A[2] := A[1] sets A[2]=32
INSERTING
• Let LA be a collection of data elements in the memory of the
computer.
• “Inserting” refers to the operation of adding another
element to the collection LA.
• Always Insertion is done at the last.
• Suppose an element is to be inserted in the middle of the
array. Then, half of the elements must be moved downwards
to new locations to accommodate the new element and
keep the order of the other elements.
Algorithm 4.2: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here
LA is a linear array with N elements and K is a positive integer such that
K<=N. the algorithm inserts an element ITEM into Kth position in LA.
1. [Initialize counter] Set J:=N
2. Repeat Steps 3 and 4 while J>=K
3. [Move Jth element downward] Set LA[J+1]:=LA[J]
4. [Decrease the counter] Set J:=J-1.
[End of Step 2 Loop]
5. [Insert the element] Set LA[K]:=ITEM
6. [Reset N] Set N:=N+1
7. Exit
LA[J+1]:=LA[J]
Set J:=J-1.
5 6
4 5
3 4
J
N is Incremented as an
element is added
DELETING
• Let LA be a collection of data elements in the memory of the
computer.
• “Deleting” refers to the operation of removing one of the
elements from LA.
• Deleting an element at the end of an array presents no
difficulties, but deleting an element somewhere in the
middle of the array would require that each subsequent
element be moved one location upward in order to “fill up”
the array.
• The following algorithm deletes the Kth element from the
linear array LA and assigns it to the variable ITEM.
Algorithm 4.3: (Deleting from a Linear Array)
DELETE (LA, N, K, ITEM). Here LA is a linear array with N
elements and K is a positive integer such that K<=N. The
algorithm deletes Kth element from LA.
1. Set ITEM:=LA[K]
2. Repeat For J=K to N-1
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of Loop]
3. [Reset the number N of elements in LA] Set N:=N-1.
4. Exit
LA[J]:=LA[J+1]
3 2
4 3
5 4
6 5
7 6
N is Decremented as an element is removed
REPRESENTATION OF POLYNOMIALS USING ARRAYS
•Sometimes it may be necessary to evaluate several
polynomial expressions to perform basic arithmetic
operations. The polynomial expressions can be stored
in arrays. All the elements of an array has two values
namely coefficient and exponent.
• Once a polynomial is built, it can be used to perform
any arithmetic operations.
• A single dimensional array is used to represent a
single variable polynomial of degree n. the index can
be considered as exponent and the coefficients can be
stored at that particular location.
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
SPARSE MATRIX
• A matrix with a relatively high proportion of zero entries is called
sparse matrix. Two general types of n-square sparse matrices
which occur in various applications are shown below.
1. The matrix where all the entries above the main diagonal are
zeros, non-zero entries may occur below the main diagonal is
called lower triangular matrix.
2. The matrix where all the entries below the main diagonal are
zeros, non-zero entries may occur above the main diagonal is
called upper triangular matrix.
3. The matrix where non-zero entries occur on the diagonal or on
elements immediately above or below the diagonal, is called
tri-diagonal matrix.
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI

More Related Content

What's hot (20)

PPTX
Array operations
ZAFAR444
 
PPTX
Doubly Linked List
V.V.Vanniaperumal College for Women
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
PPTX
Merge Sort
Nikhil Sonkamble
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
Binary search
AparnaKumari31
 
PPTX
Deque and its applications
Jsaddam Hussain
 
PPTX
Linear Search
SWATHIR72
 
PPTX
Queue - Data Structure - Notes
Omprakash Chauhan
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PDF
Array linear data_structure_2 (1)
eShikshak
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Circular queue
Lovely Professional University
 
PPTX
Queue in Data Structure
Janki Shah
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPTX
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Array operations
ZAFAR444
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Merge Sort
Nikhil Sonkamble
 
Sorting Algorithms
Pranay Neema
 
Binary search
AparnaKumari31
 
Deque and its applications
Jsaddam Hussain
 
Linear Search
SWATHIR72
 
Queue - Data Structure - Notes
Omprakash Chauhan
 
Quick sort-Data Structure
Jeanie Arnoco
 
Array linear data_structure_2 (1)
eShikshak
 
Binary search tree(bst)
Hossain Md Shakhawat
 
Data structure array
MajidHamidAli
 
Queue in Data Structure
Janki Shah
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Priority Queue in Data Structure
Meghaj Mallick
 
Arrays in Data Structure and Algorithm
KristinaBorooah
 

Similar to BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI (20)

PPTX
Data structures (Array 1 dimensional).pptx
itzsomeone50
 
PDF
DSA.pdf
AkashSaha75835
 
PPTX
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
PPTX
Arrays
DebiPrasadSen
 
PDF
Data Structures Chapter-4
priyavanimurugarajan
 
PPTX
arrays in data structure.pptx
KasthuriKAssistantPr
 
PPTX
2.DS Array
Chandan Singh
 
PPTX
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
PPT
arrays
Enakshi Chanda
 
PPT
Arrays
SARITHA REDDY
 
PDF
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
PPTX
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
PPTX
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
PPT
Arrays Data Structure
student
 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
DOCX
Updated Lab3.docx
AleezaAnjum
 
Data structures (Array 1 dimensional).pptx
itzsomeone50
 
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
Data Structures Chapter-4
priyavanimurugarajan
 
arrays in data structure.pptx
KasthuriKAssistantPr
 
2.DS Array
Chandan Singh
 
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
Arrays Data Structure
student
 
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Updated Lab3.docx
AleezaAnjum
 
Ad

More from Sowmya Jyothi (20)

PDF
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
PDF
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
PDF
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Sowmya Jyothi
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PDF
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Sowmya Jyothi
 
PDF
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
PDF
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Sowmya Jyothi
 
PDF
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PDF
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
PPTX
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
PPT
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PPT
Introduction to computers MRS. SOWMYA JYOTHI
Sowmya Jyothi
 
PPT
Introduction to graphics
Sowmya Jyothi
 
PDF
Inter Process Communication PPT
Sowmya Jyothi
 
PDF
Internal representation of file chapter 4 Sowmya Jyothi
Sowmya Jyothi
 
PDF
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
PDF
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Sowmya Jyothi
 
PPT
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Sowmya Jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Sowmya Jyothi
 
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to computers MRS. SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to graphics
Sowmya Jyothi
 
Inter Process Communication PPT
Sowmya Jyothi
 
Internal representation of file chapter 4 Sowmya Jyothi
Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Dimensions of Societal Planning in Commonism
StefanMz
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Horarios de distribución de agua en julio
pegazohn1978
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 

BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI

  • 1. Chapter-3 Linear Data Structure - Arrays MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE Reference: Data Structures with C by Seymour Lipschutz, Schaum’s Outlines Series.
  • 2. INTRODUCTION • Data structures are classified as either linear or nonlinear. • A data structure is said to be linear, if its elements form a sequence. There are two basic ways of representing linear structures in memory. 1. Using arrays. 2. Using linked lists.
  • 3. • LINEAR ARRAYS A linear array is a list of a finite number N of homogeneous data elements (i.e. data elements of the same type) such that: a) The elements of the array are referred respectively by an index set consisting of n consecutive numbers b) The elements of the array are stored respectively in successive memory locations. The number n of elements is called the length or size of the array. If not explicitly stated, we will assume the index set consists of the integers 1, 2, …. N. In general, the length or the number of data elements of the array can be obtained from the index set by the formula Length=UB – LB + 1 Where UB is the largest index, called the upper bound, and LB is the smallest index, called the lower bound of the array. (Length= UB when LB=1).
  • 4. The elements of an array A may be denoted by the subscript notation A1, A2, A3, ….. An OR by the bracket notation A[1], A[2], A[3], ….. A[n] The number K in A[K] is called a subscript or an index and A[K] is called a subscripted variable.
  • 5. Example • Let DATA be a 6-element linear array of integers such that DATA[1]=247 DATA[2]=56 DATA[3]=429 DAT[4]=135 DATA[5]=87 DATA[6]=156. • We will denote such an array by DATA: 247, 56, 429, 135, 87, 156
  • 6. Problems: 1) Consider the linear array LA(1:10). Find the number of elements in an array. Ans: The number of elements is equal to the length; hence the formula Length= UB – LB + 1 Length(LA) = 10 – 1 + 1 = 10
  • 7. 2) Consider the linear array AA(5:50), BB(-5:10) and CC(18). Find the number of elements in each array. Ans: Length= UB – LB + 1 a) Length(AA) = 50 – 5 + 1 = 46 b) Length(BB) = 10 – (-5) + 1 = 16 c) Length(CC) = 18 – 1 + 1 = 18
  • 8. Abstract Data Types (ADT) • An abstract data type refers to a set of data values and associated operations that are specified accurately, independent of implementation. • With an ADT, it is possible to know what a data type does, but how it does is hidden. • An ADT can further be defined as a data declaration packaged together with the operations that are meaningful for the data. User may not have the knowledge of data structure.
  • 9. ARRAY AS ADT • An array is a fundamental Abstract Data Type. An array is pre- defined set which has a sequence of cells where we can store different types of elements. • Consider the following example: object (A, N). Here an array A is created which can store N number of items in it. • A[i] is an item in the array stored in its ith position.
  • 10. • REPRESENTATION OF LINEAR ARRAYS IN MEMORY • Let LA be a linear array in the memory of the computer. The notation LOC(LA[K])=address of the element LA[K] of the array LA. • Let LA[10]={5, 6, 12, 4, 15, 45, 87, 1, 9, 13} and Base address be 100.
  • 11. • The elements of linear array LA are stored in successive memory locations. • Computer does not need to keep track of the address of every element of LA, but it needs the base address Base(LA). • Using the address of Base(LA), the computer calculates the address of any element of LA by the following formula: • LOC(LA[K]) = Base(LA) + w(K – Lower Bound) Where w is number of words per memory cell for the array LA
  • 12. Problems: 1) Consider the linear array AA(1:10). Suppose Base(AA)=100 and w=4 words per memory cell for AA. Find the address of LA[5]. Ans: LOC(AA[K]) = Base(AA) + w(K-LB) LOC(AA[5]) = 100 + 4 (5-1) = 100 + 16 = 116
  • 13. 2) Consider the linear array AA(5:50). Suppose Base(AA)=300 and w=4 words per memory cell for AA. Find the address of a) AA[15], b) AA[35] and c) AA[55]. LOC(AA[K]) = Base(AA) + w(K-LB) a) LOC(AA[15]) = 300 + 4 (15-5) = 300 + 40 = 340 b) LOC(AA[35]) = 300 + 4 (35-5) = 300 + 120 = 420 c) AA[55] is not an element of AA, since 55 exceeds UB=50.
  • 14. TRAVERSING A LINEAR ARRAY • Let LA be the collection of data elements stored in the memory of the computer. Suppose we want to print the contents of each element of LA or suppose we want to count the number of elements of LA with each given property. • This can be accomplished by traversing A, that is, by accessing and processing each element of the array exactly once is called traversing.
  • 15. • Algorithm 4.1: (Traverse a Linear Array) Here LA is a Linear array with lower boundary LB and upper boundary UB. This algorithm traverses LA applying an operation Process to each element of LA. 1. [Initialize counter] Set K:=LB. 2. Repeat Steps 3 and 4 while K≤UB. 3. [Visit element] Apply PROCESS to LA[K]. 4. [Increase counter] Set K:=K+1. [End of Step 2 loop] 5. Exit.
  • 16. The alternate algorithm for traversing (using for loop) is: Algorithm 4.1: (Traverse a Linear Array) This algorithm traverse a linear array LA with lower bound LB and upper bound UB. 1. Repeat for K=LB to UB Apply PROCESS to LA[K]. [End of loop] 2. Exit.
  • 17. Problems: 1) Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}. Find the values of the given arrays after each loop. a) Repeat for K = 1 to 4: Set AB[K + 1] := A[K] [End of loop] Ans: First AB[2] := A[1] sets AB[2]=25, the value of A[1] Then AB[3] := A[2] sets AB[3]=32, the value of A[2] Then AB[4] := A[3] sets AB[4]=65, the value of A[3] Then AB[5] := A[4] sets AB[5]=43, the value of A[4]
  • 18. Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}. b) Repeat for K = 4 to 1 by -1: Set A[K + 1] := A[K] [End of loop] Ans: First A[5] := A[4] sets A[5]=78 Then A[4] := A[3] sets A[4]=43 Then A[3] := A[2] sets A[3]=65 Then A[2] := A[1] sets A[2]=32
  • 19. INSERTING • Let LA be a collection of data elements in the memory of the computer. • “Inserting” refers to the operation of adding another element to the collection LA. • Always Insertion is done at the last. • Suppose an element is to be inserted in the middle of the array. Then, half of the elements must be moved downwards to new locations to accommodate the new element and keep the order of the other elements.
  • 20. Algorithm 4.2: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here LA is a linear array with N elements and K is a positive integer such that K<=N. the algorithm inserts an element ITEM into Kth position in LA. 1. [Initialize counter] Set J:=N 2. Repeat Steps 3 and 4 while J>=K 3. [Move Jth element downward] Set LA[J+1]:=LA[J] 4. [Decrease the counter] Set J:=J-1. [End of Step 2 Loop] 5. [Insert the element] Set LA[K]:=ITEM 6. [Reset N] Set N:=N+1 7. Exit
  • 21. LA[J+1]:=LA[J] Set J:=J-1. 5 6 4 5 3 4 J N is Incremented as an element is added
  • 22. DELETING • Let LA be a collection of data elements in the memory of the computer. • “Deleting” refers to the operation of removing one of the elements from LA. • Deleting an element at the end of an array presents no difficulties, but deleting an element somewhere in the middle of the array would require that each subsequent element be moved one location upward in order to “fill up” the array. • The following algorithm deletes the Kth element from the linear array LA and assigns it to the variable ITEM.
  • 23. Algorithm 4.3: (Deleting from a Linear Array) DELETE (LA, N, K, ITEM). Here LA is a linear array with N elements and K is a positive integer such that K<=N. The algorithm deletes Kth element from LA. 1. Set ITEM:=LA[K] 2. Repeat For J=K to N-1 [Move J+1st element upward] Set LA[J]:=LA[J+1] [End of Loop] 3. [Reset the number N of elements in LA] Set N:=N-1. 4. Exit
  • 24. LA[J]:=LA[J+1] 3 2 4 3 5 4 6 5 7 6 N is Decremented as an element is removed
  • 25. REPRESENTATION OF POLYNOMIALS USING ARRAYS •Sometimes it may be necessary to evaluate several polynomial expressions to perform basic arithmetic operations. The polynomial expressions can be stored in arrays. All the elements of an array has two values namely coefficient and exponent. • Once a polynomial is built, it can be used to perform any arithmetic operations. • A single dimensional array is used to represent a single variable polynomial of degree n. the index can be considered as exponent and the coefficients can be stored at that particular location.
  • 28. SPARSE MATRIX • A matrix with a relatively high proportion of zero entries is called sparse matrix. Two general types of n-square sparse matrices which occur in various applications are shown below. 1. The matrix where all the entries above the main diagonal are zeros, non-zero entries may occur below the main diagonal is called lower triangular matrix. 2. The matrix where all the entries below the main diagonal are zeros, non-zero entries may occur above the main diagonal is called upper triangular matrix. 3. The matrix where non-zero entries occur on the diagonal or on elements immediately above or below the diagonal, is called tri-diagonal matrix.