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
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.
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)

Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
Array
ArrayArray
Array
Anil Neupane
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
dincyjain
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
dincyjain
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 

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)

Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
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.