SlideShare a Scribd company logo
Arrays
Data Structures 2
 An array is a list of a finite number of
homogenous (similar data) elements.
 This means that an array can store either all
integers, all floating point numbers, all
characters (string) or any other complex data
type but all of same type.
Arrays
• There are some important points :
– Arrays are always stored in successive memory
locations.
– An array can store multiple values which can be
referenced by a single name.
– Array name is actually a pointer to the first
location of the memory block allocated to the
name of the array
– An array either be an integer, character or floating
data type can be initialized only during declaration
time, and not afterwards
– There is no bound checking concept for arrays in
C.
Data Structures 3
• Elements of an array A may be denoted by the
subscript notation
A1, A2, …………… An
Or
A[1], A[2],………..A[n]
• The number n of elements is called the length
or size of the array.
• The number n in A[n] is called a subscript /
index and A[n] is called a subscript variable.
Data Structures 4
• Length / Size of array = 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 where LB =1
Data Structures 5
Example
• An automobile company uses an array AUTO
to record the number of automobiles sold
each year from 1932 through 1984. Rather
than beginning the index set with 1, it is more
useful to begin the index set with 1932 so that
• AUTO[K] = number of automobiles sold in the
year K.
Data Structures 6
Answer
• LB = 1932 is the lower bound and UB= 1984 is
the upper bound of AUTO.
– Length = UB – LB +1
– Length = 1984 – 1932 + 1 = 53
• That is, AUTO contains 53 elements and its
index set consists of all integers from 1932
through 1984
Data Structures 7
One Dimensional Arrays
• A one dimensional array is one in which only
one subscript specification is needed to
specify a particular element of the array.
• One dimensional array can be declared as :
Data_type var_name [expression];
• Where data_type is the type of elements to be
stored in the array.
• Var_name specifies the name of array,
• Expression or subscript specifies the number
of values to be stored in the array.
Data Structures 8
• Example :
• Int num[5]
• The array will store five integer values, its
name is num
• num[0]=22, num[1] = 1, num[2]=30,
num[3]=9, num[4]=3
num
0 1 2 3 4
Data Structures 9
22 1 30 9 3
• The size of array can be determine as :
• Size = UB – LB +1
• For array num size will be :
• UB = 4
• LB = 0
• Size = 4 – 0 +1 = 5
• Size of array in bytes (i.e. amount of memory
array occupies)
Data Structures 10
• Size in bytes = size of array x size of base type
• E.g. if we have array
int num [5];
• Then size in bytes = 5 x 2 = 10 bytes
• Similarly, if we have array
float num[5];
• Then size in bytes = 5 x 4 = 20 bytes
Data Structures 11
Data Structures 12
Initializing Array
• int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements
become 0
–int n[ 5 ] = { 0 }
 All elements 0
– If too many initializers, a syntax error occurs
– C arrays have no bounds checking!!
• If size omitted, initializers determine it
 int n[ ] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore 5 element array
Data Structures 13
Addressing of element in 1-D Array
• Let A be a linear array stored in successive memory cells.
LOC(A[K]) = address of the element A[K] of the array A
1000
1001
1002
1003
• To calculate the address of any element of A formula is::
LOC(A[K])= Base (A) + w (K-lower bound)
• Base (A) =Address of the first element of A
• w =number of words per memory cell for the array
• K =any index of A
Example
• Consider the array AUTO in which records the
number of automobiles sold each year from 1932
through 1984. Suppose AUTO appears in memory as
pictured. That is Base(AUTO) = 200 and w = 4 words
per memory cell for AUTO Then
• LOC(AUTO[1932]) = 200,
• LOC(AUTO[1933])=204,
• LOC(AUTO[1934])=208,…..
• The address of the array element for the year K=1965
can be obtained by using formula.
Data Structures 14
Answer
• LOC(AUTO[1965]) = Base(AUTO) + w (1965 –
lower bound)
• LOC(AUTO[1965])= 200 + 4 (1965 – 1932)
• LOC(AUTO[1965]) = 332
Data Structures 15
Data Structures 16
Implementation of linear array
• Let LA be a linear array and stored in computer
memory .Now we want to count the no. of elements
of LA or print the contents of each element of LA,
this is done by traversing LA
• Also we want to insert an item in the linear array,
done by Insert operation
• Also we want to delete an item in the linear array,
done by Delete operation
Data Structures 17
Algo. For Traversing in linear array
• LA is a linear array with UB upper bound
and LB lower bound for traversing
we apply PROCESS to each element of LA
Steps are:
1.Set k=LB
2.Repeat step 3 and 4 while k<=UB
3.Apply PROCESS to LA [k]
4.k=k+1
5.exit
Data Structures 18
Inserting an element in array
• INSERT (LA, N, k, ITEM)
Here LA is a Linear array with N elements and K is a
positive integer. This algorithm inserts an element ITEM
into the Kth position in LA.
Steps are:
1.Set j=N
2. Repeats step 3 and 4 while j>=k
3.Set LA[j+1]=LA[j]
4.Set j=j-1
5.Set LA[k]=ITEM
6.Set N=N+1
7.Exit
Data Structures 19
Delete an element in array
• DELETE (LA, N, k, ITEM)
Here LA is a Linear array with N elements and K is a
positive integer. This algorithm deletes the Kth
element from LA.
Steps are:
1.Set ITEM = LA[k]
2. Repeat for j = k to N-1
Set LA[j] = LA[j+1]
3.Set N=N-1
4.Exit
Disadvantages of linear Array
• The number of elements, which is to be stored
in an array must be known first.
• These are static structures. Static in the sense
that memory is allocated at compilation time.
• When an array is declared, memory is
allocated to it. If memory is not filled
completely, the vacant place occupies
memory.
Data Structures 20
• The elements of the arrays are stored in
consecutive locations, the insertion and
deletion into the array are time consuming.
Because to insert/delete an item require
moving elements down to create a space for
new element or moving elements up to
occupy the space by the deleted element.
Data Structures 21
Data Structures 22
Two-Dimensional Array (Table)
Col 0 Col 1 Col 2 Col 3 Col 4
Row 0 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
Row 1 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
Row 2 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4]
►short a[3][5]; // a table with 3 rows,5 columns
►The first dimension denotes the row-index,
which starts with 0.
►The second dimension denotes the column-
index, which also starts with 0.
Data Structures 23
Two-Dimensional Array
• A two dimensional array A is a collection of m * n
elements and each element is specified by pair of integers
(such as J, K) called subscripts
1<=J<= m AND 1<=K<=n
• Element of A with first subscript j and second subscript k
will denoted by
AJ,K or A[J,K]
• Two-dimensional arrays are called matrices in
mathematics and tables in business applications.
• The element A[J,K] appears in row J and column K
• A row is a horizontal list of elements and a column is a
vertical list of elements.
• A has 3 rows and 4 columns
Columns
1 2 3 4
Data Structures 24
• Suppose A is a two-dimensional m x n array. The
first dimension of A contains the index set
1,2…..m with lower bound 1 and upper bound m
• The second dimension of A contains the index set
1,2…..n with lower bound 1 and upper bound n.
• The length of a given dimension
Length = upper bound – lower bound + 1
• Size of array = m x n
Data Structures 25
Data Structures 26
EXAMPLE
• Let A be a 2D array such that
• A(2:5,-3:1)
• Here index set of dimensions 2,3,4,5 and -3,-
2,-1,0,1
• Length of first dimension is 5-2+1=4
• Length of second dimension is 1-(-3)+1=5
• Size of A=4*5=20
Representation of 2D array in memory
• There are two ways of representing 2D array
• Row major array
• Column major array
• Row major array : The elements are stored row by row
i.e. n elements of the first row stored in first n locations,
elements of the second row stored in next n locations
and so on.
• Column major array : The elements are stored column by
column i.e. m elements of the first column stored in first
m locations, elements of the second column stored in
next m locations and so on.
Data Structures 27
Data Structures 28
Representation of 2D array in memory
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
subscriptsA
Row 1
Row2
Row-major order
Data Structures 29
Representation of 2D array in memory
(1,1)
(2,1)
(3,1)
(2,1)
(2,2)
(2,3)
subscriptsA
column 1
column2
Column major order
Data Structures 30
Address calculation in 2D array
• Let a 2D array A of m*n size, to compute the LOC (A[j,k])
using the formula
LOC (A[j,k])=Base(A)+w[(N(j-1)+(k-1)]
(row major order)
Where Base(A) = address of 1st element of A, w = words per
memory cell and N = total no. of columns in array
LOC (A[j,k])=Base(A)+w[(M(k-1)+(j-1)]
(column major order)
Where Base(A) = address of 1st element of A, w = words per
memory cell and M = total no. of rows in array
Example
• Consider the 25 x 4 matrix array SCORE.
Suppose Base(SCORE) = 200 & w= 4. Calculate
the address of SCORE = [12,3] i.e. the 12th row
and 3 column using row major order and
column major order.
Data Structures 31
Answer
• M = 25,
• N = 4
• J = 12
• K = 3
• W = 4
• Base (SCORE) =200
Data Structures 32
Using Row-major order
• LOC (SCORE[12,3]) = Base (SCORE) + w [N(J-1) + (K-1)]
= 200 + 4 [4(12-1) + (3-1)]
= 200 + 4 [4(11) + 2)]
= 200 + 4 [44 +2]
= 200 + 4 [46]
= 200 + 184
= 384
Data Structures 33
Using Column Major Order
• LOC (SCORE[12,3]) = Base (SCORE) + w [M(K-1) + (J-1)]
= 200 + 4 [25(3-1) + (12-1)]
= 200 + 4 [25(2) + 11)]
= 200 + 4 [50 +11]
= 200 + 4 [61]
= 200 + 244
= 444
Data Structures 34
Multidimensional Array
• When array can be extended to any number of
dimensions. E.g. 3D array may be defined as :
• int a[2][4][3]
• Multidimensional array also called arrays of
arrays
• Suppose C is a 3D 2 x 4 x 3 array. Then B
contains 2 x 4 x 3 = 24 elements. These
elements appear in 3 layer called pages,
column and row.
Data Structures 35
Ad

More Related Content

What's hot (20)

heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
SHEETAL WAGHMARE
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Array
ArrayArray
Array
PRN USM
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Array
ArrayArray
Array
Anil Neupane
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
Reem Alattas
 
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
Reem Alattas
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 

Similar to 2. Array in Data Structure (20)

Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Koteswari Kasireddy
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
coc7987515756
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
Chandan Singh
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
ssuser92d367
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
HarmanShergill5
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
HarsimranKaur362773
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
MamataAnilgod
 
Two Dimensional Array
Two Dimensional ArrayTwo Dimensional Array
Two Dimensional Array
dincyjain
 
Basics of Data structure using C describing basics concepts
Basics of Data structure using C describing basics conceptsBasics of Data structure using C describing basics concepts
Basics of Data structure using C describing basics concepts
shanthidl1
 
Data strutcure and annalysis topic array
Data strutcure and annalysis topic arrayData strutcure and annalysis topic array
Data strutcure and annalysis topic array
MihirMishra36
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Array
ArrayArray
Array
Fahuda E
 
Arrays
ArraysArrays
Arrays
DebiPrasadSen
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
Data Structure_Array_and_sparse matrix.pptx
Data Structure_Array_and_sparse matrix.pptxData Structure_Array_and_sparse matrix.pptx
Data Structure_Array_and_sparse matrix.pptx
pubgnewstate1620
 
Data structure and algorithm list structures
Data structure and algorithm list structuresData structure and algorithm list structures
Data structure and algorithm list structures
gangaresearch21
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
samreenghauri786
 
Data Structures - Array presentation .pptx
Data Structures - Array presentation .pptxData Structures - Array presentation .pptx
Data Structures - Array presentation .pptx
IshanKapoor26
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
coc7987515756
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
Two Dimensional Array
Two Dimensional ArrayTwo Dimensional Array
Two Dimensional Array
dincyjain
 
Basics of Data structure using C describing basics concepts
Basics of Data structure using C describing basics conceptsBasics of Data structure using C describing basics concepts
Basics of Data structure using C describing basics concepts
shanthidl1
 
Data strutcure and annalysis topic array
Data strutcure and annalysis topic arrayData strutcure and annalysis topic array
Data strutcure and annalysis topic array
MihirMishra36
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
Data Structure_Array_and_sparse matrix.pptx
Data Structure_Array_and_sparse matrix.pptxData Structure_Array_and_sparse matrix.pptx
Data Structure_Array_and_sparse matrix.pptx
pubgnewstate1620
 
Data structure and algorithm list structures
Data structure and algorithm list structuresData structure and algorithm list structures
Data structure and algorithm list structures
gangaresearch21
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
samreenghauri786
 
Data Structures - Array presentation .pptx
Data Structures - Array presentation .pptxData Structures - Array presentation .pptx
Data Structures - Array presentation .pptx
IshanKapoor26
 
Ad

More from Mandeep Singh (11)

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
Mandeep Singh
 
7. Spanning trees
7. Spanning trees7. Spanning trees
7. Spanning trees
Mandeep Singh
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
Mandeep Singh
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
Mandeep Singh
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
Mandeep Singh
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
Mandeep Singh
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
Mandeep Singh
 
Ip6 tables in linux
Ip6 tables in linuxIp6 tables in linux
Ip6 tables in linux
Mandeep Singh
 
Iptables in linux
Iptables in linuxIptables in linux
Iptables in linux
Mandeep Singh
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
Mandeep Singh
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
Mandeep Singh
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
Mandeep Singh
 
Ad

Recently uploaded (20)

OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Journal of Soft Computing in Civil Engineering
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...
Diego López-de-Ipiña González-de-Artaza
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...
Diego López-de-Ipiña González-de-Artaza
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 

2. Array in Data Structure

  • 2. Data Structures 2  An array is a list of a finite number of homogenous (similar data) elements.  This means that an array can store either all integers, all floating point numbers, all characters (string) or any other complex data type but all of same type. Arrays
  • 3. • There are some important points : – Arrays are always stored in successive memory locations. – An array can store multiple values which can be referenced by a single name. – Array name is actually a pointer to the first location of the memory block allocated to the name of the array – An array either be an integer, character or floating data type can be initialized only during declaration time, and not afterwards – There is no bound checking concept for arrays in C. Data Structures 3
  • 4. • Elements of an array A may be denoted by the subscript notation A1, A2, …………… An Or A[1], A[2],………..A[n] • The number n of elements is called the length or size of the array. • The number n in A[n] is called a subscript / index and A[n] is called a subscript variable. Data Structures 4
  • 5. • Length / Size of array = 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 where LB =1 Data Structures 5
  • 6. Example • An automobile company uses an array AUTO to record the number of automobiles sold each year from 1932 through 1984. Rather than beginning the index set with 1, it is more useful to begin the index set with 1932 so that • AUTO[K] = number of automobiles sold in the year K. Data Structures 6
  • 7. Answer • LB = 1932 is the lower bound and UB= 1984 is the upper bound of AUTO. – Length = UB – LB +1 – Length = 1984 – 1932 + 1 = 53 • That is, AUTO contains 53 elements and its index set consists of all integers from 1932 through 1984 Data Structures 7
  • 8. One Dimensional Arrays • A one dimensional array is one in which only one subscript specification is needed to specify a particular element of the array. • One dimensional array can be declared as : Data_type var_name [expression]; • Where data_type is the type of elements to be stored in the array. • Var_name specifies the name of array, • Expression or subscript specifies the number of values to be stored in the array. Data Structures 8
  • 9. • Example : • Int num[5] • The array will store five integer values, its name is num • num[0]=22, num[1] = 1, num[2]=30, num[3]=9, num[4]=3 num 0 1 2 3 4 Data Structures 9 22 1 30 9 3
  • 10. • The size of array can be determine as : • Size = UB – LB +1 • For array num size will be : • UB = 4 • LB = 0 • Size = 4 – 0 +1 = 5 • Size of array in bytes (i.e. amount of memory array occupies) Data Structures 10
  • 11. • Size in bytes = size of array x size of base type • E.g. if we have array int num [5]; • Then size in bytes = 5 x 2 = 10 bytes • Similarly, if we have array float num[5]; • Then size in bytes = 5 x 4 = 20 bytes Data Structures 11
  • 12. Data Structures 12 Initializing Array • int n[ 5 ] = { 1, 2, 3, 4, 5 }; – If not enough initializers, rightmost elements become 0 –int n[ 5 ] = { 0 }  All elements 0 – If too many initializers, a syntax error occurs – C arrays have no bounds checking!! • If size omitted, initializers determine it  int n[ ] = { 1, 2, 3, 4, 5 }; – 5 initializers, therefore 5 element array
  • 13. Data Structures 13 Addressing of element in 1-D Array • Let A be a linear array stored in successive memory cells. LOC(A[K]) = address of the element A[K] of the array A 1000 1001 1002 1003 • To calculate the address of any element of A formula is:: LOC(A[K])= Base (A) + w (K-lower bound) • Base (A) =Address of the first element of A • w =number of words per memory cell for the array • K =any index of A
  • 14. Example • Consider the array AUTO in which records the number of automobiles sold each year from 1932 through 1984. Suppose AUTO appears in memory as pictured. That is Base(AUTO) = 200 and w = 4 words per memory cell for AUTO Then • LOC(AUTO[1932]) = 200, • LOC(AUTO[1933])=204, • LOC(AUTO[1934])=208,….. • The address of the array element for the year K=1965 can be obtained by using formula. Data Structures 14
  • 15. Answer • LOC(AUTO[1965]) = Base(AUTO) + w (1965 – lower bound) • LOC(AUTO[1965])= 200 + 4 (1965 – 1932) • LOC(AUTO[1965]) = 332 Data Structures 15
  • 16. Data Structures 16 Implementation of linear array • Let LA be a linear array and stored in computer memory .Now we want to count the no. of elements of LA or print the contents of each element of LA, this is done by traversing LA • Also we want to insert an item in the linear array, done by Insert operation • Also we want to delete an item in the linear array, done by Delete operation
  • 17. Data Structures 17 Algo. For Traversing in linear array • LA is a linear array with UB upper bound and LB lower bound for traversing we apply PROCESS to each element of LA Steps are: 1.Set k=LB 2.Repeat step 3 and 4 while k<=UB 3.Apply PROCESS to LA [k] 4.k=k+1 5.exit
  • 18. Data Structures 18 Inserting an element in array • INSERT (LA, N, k, ITEM) Here LA is a Linear array with N elements and K is a positive integer. This algorithm inserts an element ITEM into the Kth position in LA. Steps are: 1.Set j=N 2. Repeats step 3 and 4 while j>=k 3.Set LA[j+1]=LA[j] 4.Set j=j-1 5.Set LA[k]=ITEM 6.Set N=N+1 7.Exit
  • 19. Data Structures 19 Delete an element in array • DELETE (LA, N, k, ITEM) Here LA is a Linear array with N elements and K is a positive integer. This algorithm deletes the Kth element from LA. Steps are: 1.Set ITEM = LA[k] 2. Repeat for j = k to N-1 Set LA[j] = LA[j+1] 3.Set N=N-1 4.Exit
  • 20. Disadvantages of linear Array • The number of elements, which is to be stored in an array must be known first. • These are static structures. Static in the sense that memory is allocated at compilation time. • When an array is declared, memory is allocated to it. If memory is not filled completely, the vacant place occupies memory. Data Structures 20
  • 21. • The elements of the arrays are stored in consecutive locations, the insertion and deletion into the array are time consuming. Because to insert/delete an item require moving elements down to create a space for new element or moving elements up to occupy the space by the deleted element. Data Structures 21
  • 22. Data Structures 22 Two-Dimensional Array (Table) Col 0 Col 1 Col 2 Col 3 Col 4 Row 0 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] Row 1 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] Row 2 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] ►short a[3][5]; // a table with 3 rows,5 columns ►The first dimension denotes the row-index, which starts with 0. ►The second dimension denotes the column- index, which also starts with 0.
  • 23. Data Structures 23 Two-Dimensional Array • A two dimensional array A is a collection of m * n elements and each element is specified by pair of integers (such as J, K) called subscripts 1<=J<= m AND 1<=K<=n • Element of A with first subscript j and second subscript k will denoted by AJ,K or A[J,K] • Two-dimensional arrays are called matrices in mathematics and tables in business applications. • The element A[J,K] appears in row J and column K • A row is a horizontal list of elements and a column is a vertical list of elements.
  • 24. • A has 3 rows and 4 columns Columns 1 2 3 4 Data Structures 24
  • 25. • Suppose A is a two-dimensional m x n array. The first dimension of A contains the index set 1,2…..m with lower bound 1 and upper bound m • The second dimension of A contains the index set 1,2…..n with lower bound 1 and upper bound n. • The length of a given dimension Length = upper bound – lower bound + 1 • Size of array = m x n Data Structures 25
  • 26. Data Structures 26 EXAMPLE • Let A be a 2D array such that • A(2:5,-3:1) • Here index set of dimensions 2,3,4,5 and -3,- 2,-1,0,1 • Length of first dimension is 5-2+1=4 • Length of second dimension is 1-(-3)+1=5 • Size of A=4*5=20
  • 27. Representation of 2D array in memory • There are two ways of representing 2D array • Row major array • Column major array • Row major array : The elements are stored row by row i.e. n elements of the first row stored in first n locations, elements of the second row stored in next n locations and so on. • Column major array : The elements are stored column by column i.e. m elements of the first column stored in first m locations, elements of the second column stored in next m locations and so on. Data Structures 27
  • 28. Data Structures 28 Representation of 2D array in memory (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) subscriptsA Row 1 Row2 Row-major order
  • 29. Data Structures 29 Representation of 2D array in memory (1,1) (2,1) (3,1) (2,1) (2,2) (2,3) subscriptsA column 1 column2 Column major order
  • 30. Data Structures 30 Address calculation in 2D array • Let a 2D array A of m*n size, to compute the LOC (A[j,k]) using the formula LOC (A[j,k])=Base(A)+w[(N(j-1)+(k-1)] (row major order) Where Base(A) = address of 1st element of A, w = words per memory cell and N = total no. of columns in array LOC (A[j,k])=Base(A)+w[(M(k-1)+(j-1)] (column major order) Where Base(A) = address of 1st element of A, w = words per memory cell and M = total no. of rows in array
  • 31. Example • Consider the 25 x 4 matrix array SCORE. Suppose Base(SCORE) = 200 & w= 4. Calculate the address of SCORE = [12,3] i.e. the 12th row and 3 column using row major order and column major order. Data Structures 31
  • 32. Answer • M = 25, • N = 4 • J = 12 • K = 3 • W = 4 • Base (SCORE) =200 Data Structures 32
  • 33. Using Row-major order • LOC (SCORE[12,3]) = Base (SCORE) + w [N(J-1) + (K-1)] = 200 + 4 [4(12-1) + (3-1)] = 200 + 4 [4(11) + 2)] = 200 + 4 [44 +2] = 200 + 4 [46] = 200 + 184 = 384 Data Structures 33
  • 34. Using Column Major Order • LOC (SCORE[12,3]) = Base (SCORE) + w [M(K-1) + (J-1)] = 200 + 4 [25(3-1) + (12-1)] = 200 + 4 [25(2) + 11)] = 200 + 4 [50 +11] = 200 + 4 [61] = 200 + 244 = 444 Data Structures 34
  • 35. Multidimensional Array • When array can be extended to any number of dimensions. E.g. 3D array may be defined as : • int a[2][4][3] • Multidimensional array also called arrays of arrays • Suppose C is a 3D 2 x 4 x 3 array. Then B contains 2 x 4 x 3 = 24 elements. These elements appear in 3 layer called pages, column and row. Data Structures 35
  翻译: