SlideShare a Scribd company logo
2
What
are Arrays?
Arrays a data structure that can store a fixed-size
sequential collection of elements of the same type.
An array is a collection of variables of the same type
i.e. it stores a collection of data of the same type.
Syntax: Data_type array_name [size];
Example: Int a[5];
Main idea behind having array:
Instead of declaring individual variables, such as
number0, number1, ..., and number99, declaring
one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to
represent individual variables is way much easier.
Most read
3
Important
Points about
Arrays
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to
the last element.
All arrays have 0 as the index of their first element which is also
called the base index and the last index of an array will be total
size of the array minus 1.
A specific element in an array is accessed by an index.
Usage: Arrays are commonly used in computer programs to organize
data so that a related set of values can be easily sorted or searched.
Also, used to Implement other data structures like Stacks, Queues,
Heaps, Hash tables, etc.
Most read
10
Multi-Dimensional
Array
Array having more than one subscript variable
is called Multi-Dimensional array. multi-
dimensional
Array is also called as Matrix. The most
common type of multi-dimensional arrays
are the 2D arrays.
The 2-D arrays are used to store data in the
form of table. They are also used to create
mathematical matrices and perform simple
arithmetical calculations.
Syntax: <data-type> <array_name>
[row_subscript][column-subscript];
Most read
Introduction to
Arrays in Data
Structures and
Algorithm
By Kristina Barooah
Computer Science Engineering, Lovely Professional University
Technical Member of GeeksforGeeks-LPU.
What
are Arrays?
Arrays a data structure that can store a fixed-size
sequential collection of elements of the same type.
An array is a collection of variables of the same type
i.e. it stores a collection of data of the same type.
Syntax: Data_type array_name [size];
Example: Int a[5];
Main idea behind having array:
Instead of declaring individual variables, such as
number0, number1, ..., and number99, declaring
one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to
represent individual variables is way much easier.
Important
Points about
Arrays
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to
the last element.
All arrays have 0 as the index of their first element which is also
called the base index and the last index of an array will be total
size of the array minus 1.
A specific element in an array is accessed by an index.
Usage: Arrays are commonly used in computer programs to organize
data so that a related set of values can be easily sorted or searched.
Also, used to Implement other data structures like Stacks, Queues,
Heaps, Hash tables, etc.
Declaration of Arrays
In C/C++, an array can be declared by specifying its type and size or by initializing it or by both.
1. Array declaration by specifying size:
int a[10];
2. Array declaration by initializing elements:
int a[] = {10, 20, 30, 40} ;
Here, in case the size of the array is omitted, an array just big enough to hold the initialization is created.
Thus, the compiler creates an array of size 4. And above is same as "int a[4] = {10, 20, 30, 40}"
3. Array declaration by specifying size and initializing elements:
int a[6] = {10, 20, 30, 40};
Here, in case the compiler creates an array of size 6, initializes first 4 elements as specified by user
and rest two elements as 0. above is same as "int a[] = {10, 20, 30, 40, 0, 0}"
An "array declaration" basically means to name the array and specify the type of its
elements. It can also define the number of elements in the array. A variable with array
type is considered a pointer to the type of the array elements.
However it is not the same in other languages!
For instance, in Java the Syntax to Declare an Array is:
 dataType[] a; (or)
 dataType []a; (or)
 dataType a[];
In JavaScript the Syntax to Declare an Array is :
const array_name = [item1, item2, ...]; or
array_name = new Array( 4 ) ;
Note: JavaScript Arrays are Heterogeneous. Unlike many other popular languages, a JavaScript Array can
hold elements of multiple data types, simultaneously
Syntax to Declare an Array, In Python :
Note: Python does not have built-in support for Arrays, but Python lists can be used
instead.
E.g.: carbrands = ["Ford", " Hyundai", "BMW"]
List can contain heterogeneous values such as integers, floats, strings, tuples, lists, and
dictionaries but they are commonly used to store collections of homogeneous objects. Python lists
are mutable sequences.
Note: all these declarations were for Single dimensional arrays only!
Assignment of Arrays: Assigning an array basically means to set the
values of an array after its declaration. The assignment operator (=)
is used to assign the values to each index of an array and often a loop
is used to do the assignment of an array. The assignment of an array is
moreorless the same in all the programming languages.
For example(code in JAVA):
for (int i = 0; i < javaArray.length; i++)
{ javaArray[i] = i;
System.out.print(javaArray[i] + " ");
}
Initialization of Arrays: As the name suggests initialization means
setting the initial values to the array. It is the process of assigning
values to the array elements at the time of declaration itself. Similar
to assignment, initialization is simple and is moreorless the same in all the
programming languages.
Syntax: Datatype array_name [dimensions] = { element1, element2,
….,element N}
For example(code in JAVA):
{int a[]={33,3,4,5};
for(int i=0;i<a.length;i++)
System.out.println(a[i]);}
Accessing array Elements: Array elements can be accessed randomly by using
an integer index. Since we know, array index starts with 0 and goes till size of array
minus 1, specifying the required index value can help us access its value.
For example(code in JAVA):
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
System.out.println(arr[1]);
Types of
Arrays
Single Dimensional
Arrays
Multi-dimensional
Arrays
Single
Dimensional
Array
Single or One Dimensional array is used
to represent and store data in a linear
form. Array having only one subscript
variable is called One-Dimensional array
or 1-D array or Linear array.
Syntax: <data-type> <arrayname> [size];
For example:
int iarr[3] = {2, 3, 4};
char carr[20] = "c4learn";
float farr[3] = {12.5,13.5,14.5};
Multi-Dimensional
Array
Array having more than one subscript variable
is called Multi-Dimensional array. multi-
dimensional
Array is also called as Matrix. The most
common type of multi-dimensional arrays
are the 2D arrays.
The 2-D arrays are used to store data in the
form of table. They are also used to create
mathematical matrices and perform simple
arithmetical calculations.
Syntax: <data-type> <array_name>
[row_subscript][column-subscript];
1. Declaration of Two Dimensional Array:
Syntax: datatype arrayName [ rowSize ] [ columnSize ] ;
For example: int matrix_A [2][3] ;
2. Initialization of Two Dimensional Array:
Syntax: Data type array Name [rows][colmns] =
{{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;
For example: int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
3. Accessing Individual Elements of Two Dimensional
Array:
Syntax: Array Name [ row Index ] [ column Index ]
For example: matrix [0][1] = 10 ;
Basic
Operations
• Traverse − print all the array
elements one by one.
• Insertion − Adds an element at the
given index.
• Deletion − Deletes an element at the
given index.
• Search − Searches an element using
the given index or by the value.
• Update − Updates an element at the
given index.

More Related Content

What's hot (20)

Computer instructions
Computer instructionsComputer instructions
Computer instructions
Anuj Modi
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Indexing Data Structure
Indexing Data StructureIndexing Data Structure
Indexing Data Structure
Vivek Kantariya
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Types of Addressing modes- COA
Types of Addressing modes- COATypes of Addressing modes- COA
Types of Addressing modes- COA
Ruchi Maurya
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Microoperations
MicrooperationsMicrooperations
Microoperations
Rakesh Pillai
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Instruction Formats
Instruction FormatsInstruction Formats
Instruction Formats
RaaviKapoor
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
Ashish Arun
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Computer instructions
Computer instructionsComputer instructions
Computer instructions
Anuj Modi
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Types of Addressing modes- COA
Types of Addressing modes- COATypes of Addressing modes- COA
Types of Addressing modes- COA
Ruchi Maurya
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Instruction Formats
Instruction FormatsInstruction Formats
Instruction Formats
RaaviKapoor
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 

Similar to Arrays in Data Structure and Algorithm (20)

Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
lakshmanarao027MVGRC
 
Arrays
ArraysArrays
Arrays
ViniVini48
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Arrays
ArraysArrays
Arrays
Manthan Dhavne
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
akila m
 
Basic Arrays in C Programming Language I
Basic Arrays in C Programming Language IBasic Arrays in C Programming Language I
Basic Arrays in C Programming Language I
ChobodiDamsaraniPadm
 
Arrays
ArraysArrays
Arrays
swathi reddy
 
Arrays a detailed explanation and presentation
Arrays a detailed explanation and presentationArrays a detailed explanation and presentation
Arrays a detailed explanation and presentation
riazahamed37
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
 
4.arrays
4.arrays4.arrays
4.arrays
Hardik gupta
 
Arrays
ArraysArrays
Arrays
Notre Dame of Midsayap College
 
Module_3_Arrays - Updated.pptx............
Module_3_Arrays - Updated.pptx............Module_3_Arrays - Updated.pptx............
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
UNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxUNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
Arrays
ArraysArrays
Arrays
shillpi29
 
Data Structures: A Foundation for Efficient Programming
Data Structures: A Foundation for Efficient ProgrammingData Structures: A Foundation for Efficient Programming
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
akila m
 
Basic Arrays in C Programming Language I
Basic Arrays in C Programming Language IBasic Arrays in C Programming Language I
Basic Arrays in C Programming Language I
ChobodiDamsaraniPadm
 
Arrays a detailed explanation and presentation
Arrays a detailed explanation and presentationArrays a detailed explanation and presentation
Arrays a detailed explanation and presentation
riazahamed37
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Module_3_Arrays - Updated.pptx............
Module_3_Arrays - Updated.pptx............Module_3_Arrays - Updated.pptx............
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
UNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxUNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
Data Structures: A Foundation for Efficient Programming
Data Structures: A Foundation for Efficient ProgrammingData Structures: A Foundation for Efficient Programming
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
Ad

Recently uploaded (20)

Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
All about the Snail Power Catalog Product 2025
All about the Snail Power Catalog  Product 2025All about the Snail Power Catalog  Product 2025
All about the Snail Power Catalog Product 2025
kstgroupvn
 
Fresh concrete Workability Measurement
Fresh concrete  Workability  MeasurementFresh concrete  Workability  Measurement
Fresh concrete Workability Measurement
SasiVarman5
 
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
ManiMaran230751
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdfISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdfISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
What is dbms architecture, components of dbms architecture and types of dbms ...
What is dbms architecture, components of dbms architecture and types of dbms ...What is dbms architecture, components of dbms architecture and types of dbms ...
What is dbms architecture, components of dbms architecture and types of dbms ...
cyhuutjdoazdwrnubt
 
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete ColumnsAxial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Journal of Soft Computing in Civil Engineering
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
All about the Snail Power Catalog Product 2025
All about the Snail Power Catalog  Product 2025All about the Snail Power Catalog  Product 2025
All about the Snail Power Catalog Product 2025
kstgroupvn
 
Fresh concrete Workability Measurement
Fresh concrete  Workability  MeasurementFresh concrete  Workability  Measurement
Fresh concrete Workability Measurement
SasiVarman5
 
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
Digital Crime – Substantive Criminal Law – General Conditions – Offenses – In...
ManiMaran230751
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
What is dbms architecture, components of dbms architecture and types of dbms ...
What is dbms architecture, components of dbms architecture and types of dbms ...What is dbms architecture, components of dbms architecture and types of dbms ...
What is dbms architecture, components of dbms architecture and types of dbms ...
cyhuutjdoazdwrnubt
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
Ad

Arrays in Data Structure and Algorithm

  • 1. Introduction to Arrays in Data Structures and Algorithm By Kristina Barooah Computer Science Engineering, Lovely Professional University Technical Member of GeeksforGeeks-LPU.
  • 2. What are Arrays? Arrays a data structure that can store a fixed-size sequential collection of elements of the same type. An array is a collection of variables of the same type i.e. it stores a collection of data of the same type. Syntax: Data_type array_name [size]; Example: Int a[5]; Main idea behind having array: Instead of declaring individual variables, such as number0, number1, ..., and number99, declaring one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables is way much easier.
  • 3. Important Points about Arrays All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1. A specific element in an array is accessed by an index. Usage: Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched. Also, used to Implement other data structures like Stacks, Queues, Heaps, Hash tables, etc.
  • 4. Declaration of Arrays In C/C++, an array can be declared by specifying its type and size or by initializing it or by both. 1. Array declaration by specifying size: int a[10]; 2. Array declaration by initializing elements: int a[] = {10, 20, 30, 40} ; Here, in case the size of the array is omitted, an array just big enough to hold the initialization is created. Thus, the compiler creates an array of size 4. And above is same as "int a[4] = {10, 20, 30, 40}" 3. Array declaration by specifying size and initializing elements: int a[6] = {10, 20, 30, 40}; Here, in case the compiler creates an array of size 6, initializes first 4 elements as specified by user and rest two elements as 0. above is same as "int a[] = {10, 20, 30, 40, 0, 0}" An "array declaration" basically means to name the array and specify the type of its elements. It can also define the number of elements in the array. A variable with array type is considered a pointer to the type of the array elements.
  • 5. However it is not the same in other languages! For instance, in Java the Syntax to Declare an Array is:  dataType[] a; (or)  dataType []a; (or)  dataType a[]; In JavaScript the Syntax to Declare an Array is : const array_name = [item1, item2, ...]; or array_name = new Array( 4 ) ; Note: JavaScript Arrays are Heterogeneous. Unlike many other popular languages, a JavaScript Array can hold elements of multiple data types, simultaneously Syntax to Declare an Array, In Python : Note: Python does not have built-in support for Arrays, but Python lists can be used instead. E.g.: carbrands = ["Ford", " Hyundai", "BMW"] List can contain heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries but they are commonly used to store collections of homogeneous objects. Python lists are mutable sequences. Note: all these declarations were for Single dimensional arrays only!
  • 6. Assignment of Arrays: Assigning an array basically means to set the values of an array after its declaration. The assignment operator (=) is used to assign the values to each index of an array and often a loop is used to do the assignment of an array. The assignment of an array is moreorless the same in all the programming languages. For example(code in JAVA): for (int i = 0; i < javaArray.length; i++) { javaArray[i] = i; System.out.print(javaArray[i] + " "); } Initialization of Arrays: As the name suggests initialization means setting the initial values to the array. It is the process of assigning values to the array elements at the time of declaration itself. Similar to assignment, initialization is simple and is moreorless the same in all the programming languages.
  • 7. Syntax: Datatype array_name [dimensions] = { element1, element2, ….,element N} For example(code in JAVA): {int a[]={33,3,4,5}; for(int i=0;i<a.length;i++) System.out.println(a[i]);} Accessing array Elements: Array elements can be accessed randomly by using an integer index. Since we know, array index starts with 0 and goes till size of array minus 1, specifying the required index value can help us access its value. For example(code in JAVA): int[] arr = new int[3]; arr[0] = 10; arr[1] = 20; arr[2] = 30; System.out.println(arr[1]);
  • 9. Single Dimensional Array Single or One Dimensional array is used to represent and store data in a linear form. Array having only one subscript variable is called One-Dimensional array or 1-D array or Linear array. Syntax: <data-type> <arrayname> [size]; For example: int iarr[3] = {2, 3, 4}; char carr[20] = "c4learn"; float farr[3] = {12.5,13.5,14.5};
  • 10. Multi-Dimensional Array Array having more than one subscript variable is called Multi-Dimensional array. multi- dimensional Array is also called as Matrix. The most common type of multi-dimensional arrays are the 2D arrays. The 2-D arrays are used to store data in the form of table. They are also used to create mathematical matrices and perform simple arithmetical calculations. Syntax: <data-type> <array_name> [row_subscript][column-subscript];
  • 11. 1. Declaration of Two Dimensional Array: Syntax: datatype arrayName [ rowSize ] [ columnSize ] ; For example: int matrix_A [2][3] ; 2. Initialization of Two Dimensional Array: Syntax: Data type array Name [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ; For example: int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ; 3. Accessing Individual Elements of Two Dimensional Array: Syntax: Array Name [ row Index ] [ column Index ] For example: matrix [0][1] = 10 ;
  • 12. Basic Operations • Traverse − print all the array elements one by one. • Insertion − Adds an element at the given index. • Deletion − Deletes an element at the given index. • Search − Searches an element using the given index or by the value. • Update − Updates an element at the given index.