Mastering Array in C++:
Basics to Advanced
Operations
Prepared by:
Garima Saxena
Date:
23/12/2024
(Full Explanation)
INTROUCTION TO ARRAYs
An Array is a linear data structure that stores multiple elements of the same type
in contiguous memory location. It allows efficient access to element using an index,
making it ideal for managing large datasets.
What is an Array?
Arrays are widely used in programming for organizing and manipulating data in an
ordered format.
Array elements are accessed using a zero-based index.
The size of an array is fixed during its declaration.
Arrays can only store element of the same data type
Element of an array are stored in contiguous memory locations.
Array can be initialized at the time declaration.
Example: int arr[3] = {1, 2, 3};
Storing and managing data like student marks, employee records etc.
Features of arrays
Contiguous Memory Allocation:
All element of an array are stored in contiguous (adjacent) memory locations.
Fixed Size:
The size of an array is determined the time of declaration and cannot be changed
during execution.
Homogeneous Data:
Array can only store element using its index allows faster operations compared to
linked data structures.
Static Data Structure:
Arrays have a fixed size and memory is allocated during compile time.
Ease To Traverse:
Element can be easily traversed using loops.
Reusability: Array can be reused throughout the program whenever needed.
Types of array
Single Dimensional Array:
A single-dimensional array (1 D array) is a linear collection of elements stored in
contiguous memory locations. It is used to store a fixed number of elements of the
same data types.
Structures: Looks like a simple list.
Example:
Indexing:Element are accessed using an index , starting from 0.
Example: arr[0] accessed the first element
Int arr[5] = {41, 42, 43, 44, 45};
Size: Size is fixed at the time of declaration and cannot be changed later.
Initialization: You can initialize an array at the time of declaration.
Traversal: Use a loop to iterate over elements.
Example: for(int i = 0; I < 5; i++){
cout<<arr[i];
Double Dimensional Array:
A double-dimensional array (2D array) s a collection of elements arranged in rows and
columns, similar to a matrix. It is used to store data in tabular format.
Structure: it consists of rows and columns.
Example: int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; This represents 2 rows and 3 column.
Size: The size is fixed during declaration.
Example: arr[rows][columns]
Initialization: ( Direct Initialization ):
Int arr[2][3] = { {1, 2, 3}, {4, 5, 6} ;
Default initialization: All elements are set to 0 if no values are provided.
Traversal: use nested loops to access elements.
for(int i = 0; i < 2; i++){
for(int j = 0; j <3; j++){
cout<<arr[i][j]<<“ “;
} }
Format:
Operation on array
Here’s an example of common operations you can perform on arrays.
Insertion: Inserting a new element into an array.
int arr[] = {1, 2, 3, 4, 5};
cout<<“array elements: “;
for(int i=0; i<5; i++){
cout<<arr[i]<<“ “;
}
Deletion: Deleting an element of the array.
Traversal: Accessing an element from the array.
Searching: finding a specific element in the array.
Sorting: Sorting the array in ascending and descending order.
Reversing: reversing the order of element in the array.
Advantage of arrays
Fixed Memory Allocation
Easy Traversal
Sorting and Searching
Multi-Dimensional Storage
Efficient Access
Disadvantage of arrays
Insertion and Deletion Complexity
Homogeneous Data
Memory Wastage
Fixed Size
"Understanding Arrays in Data Structures: A Beginners Guide."

"Understanding Arrays in Data Structures: A Beginners Guide."

  • 1.
    Mastering Array inC++: Basics to Advanced Operations Prepared by: Garima Saxena Date: 23/12/2024 (Full Explanation)
  • 2.
    INTROUCTION TO ARRAYs AnArray is a linear data structure that stores multiple elements of the same type in contiguous memory location. It allows efficient access to element using an index, making it ideal for managing large datasets. What is an Array? Arrays are widely used in programming for organizing and manipulating data in an ordered format. Array elements are accessed using a zero-based index. The size of an array is fixed during its declaration. Arrays can only store element of the same data type Element of an array are stored in contiguous memory locations. Array can be initialized at the time declaration. Example: int arr[3] = {1, 2, 3}; Storing and managing data like student marks, employee records etc.
  • 3.
    Features of arrays ContiguousMemory Allocation: All element of an array are stored in contiguous (adjacent) memory locations. Fixed Size: The size of an array is determined the time of declaration and cannot be changed during execution. Homogeneous Data: Array can only store element using its index allows faster operations compared to linked data structures. Static Data Structure: Arrays have a fixed size and memory is allocated during compile time. Ease To Traverse: Element can be easily traversed using loops. Reusability: Array can be reused throughout the program whenever needed.
  • 5.
  • 6.
    Single Dimensional Array: Asingle-dimensional array (1 D array) is a linear collection of elements stored in contiguous memory locations. It is used to store a fixed number of elements of the same data types. Structures: Looks like a simple list. Example: Indexing:Element are accessed using an index , starting from 0. Example: arr[0] accessed the first element Int arr[5] = {41, 42, 43, 44, 45}; Size: Size is fixed at the time of declaration and cannot be changed later. Initialization: You can initialize an array at the time of declaration. Traversal: Use a loop to iterate over elements. Example: for(int i = 0; I < 5; i++){ cout<<arr[i];
  • 8.
    Double Dimensional Array: Adouble-dimensional array (2D array) s a collection of elements arranged in rows and columns, similar to a matrix. It is used to store data in tabular format. Structure: it consists of rows and columns. Example: int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; This represents 2 rows and 3 column. Size: The size is fixed during declaration. Example: arr[rows][columns] Initialization: ( Direct Initialization ): Int arr[2][3] = { {1, 2, 3}, {4, 5, 6} ; Default initialization: All elements are set to 0 if no values are provided. Traversal: use nested loops to access elements. for(int i = 0; i < 2; i++){ for(int j = 0; j <3; j++){ cout<<arr[i][j]<<“ “; } } Format:
  • 10.
    Operation on array Here’san example of common operations you can perform on arrays. Insertion: Inserting a new element into an array. int arr[] = {1, 2, 3, 4, 5}; cout<<“array elements: “; for(int i=0; i<5; i++){ cout<<arr[i]<<“ “; } Deletion: Deleting an element of the array.
  • 11.
    Traversal: Accessing anelement from the array. Searching: finding a specific element in the array.
  • 12.
    Sorting: Sorting thearray in ascending and descending order. Reversing: reversing the order of element in the array.
  • 13.
    Advantage of arrays FixedMemory Allocation Easy Traversal Sorting and Searching Multi-Dimensional Storage Efficient Access Disadvantage of arrays Insertion and Deletion Complexity Homogeneous Data Memory Wastage Fixed Size