Arrays C++
Arrays C++
Study Materials
Array:
• A collection of elements of the same data type stored in contiguous memory
locations.
• Accessed using an index. Basically they are two type of array.
• One - dimensional Array
• Multi - dimensional Array
1D Array:
• A 1D array is a linear sequence of elements.
• The one-dimensional array basically consists of a list of variables that have the
very same data type.
• It has only one dimension.
• One can easily receive it in a pointer, an un sized array, or a sized array.
• Total number of Bytes = The size of array x the size of array variable or data type.
Syntax:
Example:
Operations on 1D Arrays:
• Fixed Size: The size of an array is static and must be defined at compile-time.
• This can lead to memory wastage if the array is not fully utilized or insufficient
memory allocation if more elements are needed.
• Lack of Flexibility: Arrays do not provide dynamic resizing, making it difficult to
manage varying amounts of data without using dynamic memory allocation
• No Built-in Boundary Checking: Accessing elements outside the array's bounds
can lead to undefined behavior, including memory corruption, as C++ does not
perform automatic boundary checking.
• Limited Functionality: Arrays are basic structures with no built-in methods for
common operations like searching, sorting, or resizing, which are provided by
higher-level data structures like vectors or lists
2D Arrays:
• Row-wise Traversal:
Advantages:
• Structured Data Representation: 2D arrays allow for the representation of
tabular data, making them ideal for matrices, grids, and game boards.
• Efficient Memory Utilization: Similar to 1D arrays, 2D arrays also benefit from
contiguous memory allocation, ensuring efficient memory use and access times.
• Simplified Multidimensional Data Management: 2D arrays simplify the
management and manipulation of data that naturally fits into rows and columns
• Direct Access: Just like 1D arrays, 2D arrays allow direct access to any element
using its row and column indices, leading to efficient data retrieval.
Disadvantages: