SlideShare a Scribd company logo
Array Data Structure
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).
The above image can be looked as a top-level view of a staircase where you are at the
base of the staircase. Each element can be uniquely identified by their index in the array
(in a similar way as you could identify your friends by the step on which they were on in
the above example).
Arrays in C/C++
Last Updated: 28-04-2020
An array in C or C++ is a collection of items stored at contiguous memory locations and
elements can be accessed randomly using indices of an array. They are used to store
similar type of elements as in the data type must be the same for all elements. They can
be used to store collection of primitive data types such as int, float, double, char, etc of
any particular type. To add to it, an array in C or C++ can store derived data types such
as the structures, pointers etc. Given below is the picturesque representation of an
array.
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but
if we want to store a large number of instances, it becomes difficult to manage them
with normal variables. The idea of an array is to represent many instances in one
variable.
Array declaration in C/C++:
There are various ways in which we can declare an array. It can be done by specifying
its type and size, by initializing it or both.
1. Array declaration by specifying size
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
2. Array declaration by initializing elements
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
3. Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
// 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 arr[] = {10, 20, 30, 40, 0, 0}"
Advantages of an Array in C/C++:
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of
code.
Disadvantages of an Array in C/C++:
1. Allows a fixed number of elements to be entered which is decided at the
time of declaration. Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are
needed to be managed in accordance with the new memory allocation.
Facts about Array in C/C++:
● Accessing Array Elements:
Array elements are accessed by using an integer index. Array index starts
with 0 and goes till size of array minus 1.
2D array
In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays. For example,
float​ x[​3​][​4​];
Here, ​x​ is a two-dimensional (2d) array. The array can hold 12 elements. You can think
the array as a table with 3 rows and each row has 4 columns.
Similarly, you can declare a three-dimensional (3d) array. For example,
float​ y[​2​][​4​][​3​];
Here, the array ​y​ can hold 24 elements.
Initializing a multidimensional array
Here is how you can initialize two-dimensional and three-dimensional arrays:
Initialization of a 2d array
// Different ways to initialize two-dimensional array
int​ c[​2​][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
int​ c[][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
int​ c[​2​][​3​] = {​1​, ​3​, ​0​, ​-1​, ​5​, ​9​};
Address calculation
1D array
Loc(a[i]) = BA + (i-lb)*c
Location of an array ‘a’ for the ith index where:
BA- base address
Lb- lower bound
C- integer size
Array Data Structure for programing language
Array Data Structure for programing language
2D array
RMO: Loc(a[i][j]) = BA + [(i-lb1)*nc + (j-lb2)]c
CMO: ​Loc(a[i][j]) = BA + [(i-lb1)+(j-lb2)nr]c
Where location of element in the position of i(row) and j(column) for an array ‘a’
Where:
BA- base address
Lb1- lower bound in row
Lb2- lower bound in column
Nc- number of column
Nr- number of row
Finding number of any element: last-first +1
Array Data Structure for programing language
Array Data Structure for programing language

More Related Content

Similar to Array Data Structure for programing language (20)

PPTX
Chapter-Five.pptx
berekethailu2
 
PDF
Arrays in C++
Maliha Mehr
 
PPTX
Arrays in c language
tanmaymodi4
 
PPTX
Arrays in c v1 09102017
Tanmay Modi
 
PDF
Arraysincv109102017 180831194256
ABHAY9616302301
 
PPTX
Arrays presentation by Mam Iqra Kanwal.pptx
ibrahimkori
 
PPTX
Module1_arrays.pptx
HishamE1
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PPTX
arrays.pptx
sami717280
 
PPTX
Pooja
Ravi Ramchandani
 
PPTX
Pooja
Ravi Ramchandani
 
PPTX
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
DOCX
arrays.docx
lakshmanarao027MVGRC
 
PPT
نموذج لعمل المشروع المطلوب لمقرر برمجة الحاسب
ymalli
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPT
Mesics lecture 8 arrays in 'c'
eShikshak
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPTX
Array in C
adityas29
 
PPTX
Arrays In C Programming Explained
Simplilearn
 
PPT
C++ Arrays
أحمد محمد
 
Chapter-Five.pptx
berekethailu2
 
Arrays in C++
Maliha Mehr
 
Arrays in c language
tanmaymodi4
 
Arrays in c v1 09102017
Tanmay Modi
 
Arraysincv109102017 180831194256
ABHAY9616302301
 
Arrays presentation by Mam Iqra Kanwal.pptx
ibrahimkori
 
Module1_arrays.pptx
HishamE1
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
arrays.pptx
sami717280
 
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
نموذج لعمل المشروع المطلوب لمقرر برمجة الحاسب
ymalli
 
C++ lecture 04
HNDE Labuduwa Galle
 
Mesics lecture 8 arrays in 'c'
eShikshak
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Array in C
adityas29
 
Arrays In C Programming Explained
Simplilearn
 
C++ Arrays
أحمد محمد
 

More from deepuranjankumar08 (8)

PPTX
DSA MCA Stack-and-Queue-Data-Structures.pptx
deepuranjankumar08
 
PPTX
JAVA AWA S SEMG HAINH JAVA mar chandan.pptx
deepuranjankumar08
 
PPTX
Introduction to HTML+CSS+Javascript by Deepu.pptx
deepuranjankumar08
 
PPTX
Html,Css,Js INTERNSHIP REPORT By SELF pptx
deepuranjankumar08
 
PPTX
file system vs Dbms file system vs Dbmsppt.pptx
deepuranjankumar08
 
PPT
5g-technology-ppt for seminar report-.ppt
deepuranjankumar08
 
PPTX
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
PPTX
PYTHON inheritance PPT.pptxpython inheritance ppt and inheritance in python
deepuranjankumar08
 
DSA MCA Stack-and-Queue-Data-Structures.pptx
deepuranjankumar08
 
JAVA AWA S SEMG HAINH JAVA mar chandan.pptx
deepuranjankumar08
 
Introduction to HTML+CSS+Javascript by Deepu.pptx
deepuranjankumar08
 
Html,Css,Js INTERNSHIP REPORT By SELF pptx
deepuranjankumar08
 
file system vs Dbms file system vs Dbmsppt.pptx
deepuranjankumar08
 
5g-technology-ppt for seminar report-.ppt
deepuranjankumar08
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
PYTHON inheritance PPT.pptxpython inheritance ppt and inheritance in python
deepuranjankumar08
 
Ad

Recently uploaded (20)

PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PPTX
darshai cross section and river section analysis
muk7971
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PPTX
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
darshai cross section and river section analysis
muk7971
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Artificial Intelligence jejeiejj3iriejrjifirirjdjeie
VikingsGaming2
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
Ad

Array Data Structure for programing language

  • 1. Array Data Structure An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The above image can be looked as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by their index in the array (in a similar way as you could identify your friends by the step on which they were on in the above example). Arrays in C/C++ Last Updated: 28-04-2020 An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C or C++ can store derived data types such as the structures, pointers etc. Given below is the picturesque representation of an array.
  • 2. Why do we need arrays? We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable. Array declaration in C/C++: There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both.
  • 3. 1. Array declaration by specifying size // Array declaration by specifying size int arr1[10]; // With recent C/C++ versions, we can also // declare an array of user specified size int n = 10; int arr2[n]; 2. Array declaration by initializing elements // Array declaration by initializing elements int arr[] = { 10, 20, 30, 40 } // Compiler creates an array of size 4. // above is same as "int arr[4] = {10, 20, 30, 40}" 3. Array declaration by specifying size and initializing elements // Array declaration by specifying size and initializing // elements int arr[6] = { 10, 20, 30, 40 } // 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 arr[] = {10, 20, 30, 40, 0, 0}" Advantages of an Array in C/C++: 1. Random access of elements using array index. 2. Use of less line of code as it creates a single array of multiple elements. 3. Easy access to all the elements. 4. Traversal through the array becomes easy using a single loop.
  • 4. 5. Sorting becomes easy as it can be accomplished by writing less line of code. Disadvantages of an Array in C/C++: 1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic. 2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Facts about Array in C/C++: ● Accessing Array Elements: Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.
  • 5. 2D array In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float​ x[​3​][​4​]; Here, ​x​ is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as a table with 3 rows and each row has 4 columns. Similarly, you can declare a three-dimensional (3d) array. For example, float​ y[​2​][​4​][​3​]; Here, the array ​y​ can hold 24 elements. Initializing a multidimensional array Here is how you can initialize two-dimensional and three-dimensional arrays: Initialization of a 2d array // Different ways to initialize two-dimensional array int​ c[​2​][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}}; int​ c[][​3​] = {{​1​, ​3​, ​0​}, {​-1​, ​5​, ​9​}};
  • 6. int​ c[​2​][​3​] = {​1​, ​3​, ​0​, ​-1​, ​5​, ​9​}; Address calculation 1D array Loc(a[i]) = BA + (i-lb)*c Location of an array ‘a’ for the ith index where: BA- base address Lb- lower bound C- integer size
  • 9. 2D array RMO: Loc(a[i][j]) = BA + [(i-lb1)*nc + (j-lb2)]c CMO: ​Loc(a[i][j]) = BA + [(i-lb1)+(j-lb2)nr]c Where location of element in the position of i(row) and j(column) for an array ‘a’ Where: BA- base address Lb1- lower bound in row
  • 10. Lb2- lower bound in column Nc- number of column Nr- number of row Finding number of any element: last-first +1