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)

"Understanding Arrays in Data Structures: A Beginners Guide."
"Understanding Arrays in Data Structures: A Beginners Guide.""Understanding Arrays in Data Structures: A Beginners Guide."
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
10 arrays and pointers.pptx for array and pointer
10 arrays and pointers.pptx for array and pointer10 arrays and pointers.pptx for array and pointer
10 arrays and pointers.pptx for array and pointer
divyamth2019
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
KirubelWondwoson1
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
ABHAY9616302301
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
tanmaymodi4
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithms
Nico Ludwig
 
Arrays accessing using for loops
Arrays accessing using for loopsArrays accessing using for loops
Arrays accessing using for loops
sangrampatil81
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Arrays
ArraysArrays
Arrays
VenkataRangaRaoKommi1
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Arrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.pptArrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Arrays
ArraysArrays
Arrays
Steven Wallach
 
"Understanding Arrays in Data Structures: A Beginners Guide."
"Understanding Arrays in Data Structures: A Beginners Guide.""Understanding Arrays in Data Structures: A Beginners Guide."
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
10 arrays and pointers.pptx for array and pointer
10 arrays and pointers.pptx for array and pointer10 arrays and pointers.pptx for array and pointer
10 arrays and pointers.pptx for array and pointer
divyamth2019
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
KirubelWondwoson1
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
ABHAY9616302301
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
tanmaymodi4
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithms
Nico Ludwig
 
Arrays accessing using for loops
Arrays accessing using for loopsArrays accessing using for loops
Arrays accessing using for loops
sangrampatil81
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Arrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.pptArrays and vectors in Data Structure.ppt
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 

More from deepuranjankumar08 (8)

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

Recently uploaded (20)

Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
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
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
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
 
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
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
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
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
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
 
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
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
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
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
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
 
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
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
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
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
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
 
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
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
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