SlideShare a Scribd company logo
2
Most read
3
Most read
12
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)

PDF
Array data structure
maamir farooq
 
PPTX
Introduction to Array ppt
sandhya yadav
 
PPTX
Abstract Data Types
karthikeyanC40
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PPT
Linked lists
SARITHA REDDY
 
PPTX
Array ppt
Kaushal Mehta
 
PPTX
Data structure - Graph
Madhu Bala
 
PPT
Arrays
SARITHA REDDY
 
PPT
Binary Search
kunj desai
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPTX
Tree - Data Structure
Ashim Lamichhane
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPT
Arrays
archikabhatia
 
PPT
Enumerated data types in C
Arpana shree
 
PPTX
Array in c++
Mahesha Mano
 
PPTX
stack & queue
manju rani
 
PPTX
Arrays In C++
Awais Alam
 
PPTX
Arrays in c
Jeeva Nanthini
 
Array data structure
maamir farooq
 
Introduction to Array ppt
sandhya yadav
 
Abstract Data Types
karthikeyanC40
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Linked lists
SARITHA REDDY
 
Array ppt
Kaushal Mehta
 
Data structure - Graph
Madhu Bala
 
Binary Search
kunj desai
 
sorting and its types
SIVASHANKARIRAJAN
 
Tree - Data Structure
Ashim Lamichhane
 
single linked list
Sathasivam Rangasamy
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Enumerated data types in C
Arpana shree
 
Array in c++
Mahesha Mano
 
stack & queue
manju rani
 
Arrays In C++
Awais Alam
 
Arrays in c
Jeeva Nanthini
 

Similar to Arrays in Data Structure and Algorithm (20)

PPT
Mesics lecture 8 arrays in 'c'
eShikshak
 
DOCX
arrays.docx
lakshmanarao027MVGRC
 
PDF
Arrays
ViniVini48
 
PDF
Arrays-Computer programming
nmahi96
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PPT
Arrays Basics
Nikhil Pandit
 
PPT
Arrays
Manthan Dhavne
 
PPTX
ARRAYS.pptx
akila m
 
PPTX
Basic Arrays in C Programming Language I
ChobodiDamsaraniPadm
 
PPT
Arrays
swathi reddy
 
PDF
Arrays a detailed explanation and presentation
riazahamed37
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
4.arrays
Hardik gupta
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
PPTX
Arrays
shillpi29
 
PPT
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
PPT
arrayy.ppt
ssuserb82af5
 
Mesics lecture 8 arrays in 'c'
eShikshak
 
Arrays
ViniVini48
 
Arrays-Computer programming
nmahi96
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Arrays Basics
Nikhil Pandit
 
ARRAYS.pptx
akila m
 
Basic Arrays in C Programming Language I
ChobodiDamsaraniPadm
 
Arrays
swathi reddy
 
Arrays a detailed explanation and presentation
riazahamed37
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
4.arrays
Hardik gupta
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
Arrays
shillpi29
 
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
arrayy.ppt
ssuserb82af5
 
Ad

Recently uploaded (20)

PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PDF
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPTX
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
PDF
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
Functions in Python Programming Language
BeulahS2
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
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.