0% found this document useful (0 votes)
7 views

Arrays C++

Arrays

Uploaded by

ash.n.mc.57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays C++

Arrays

Uploaded by

ash.n.mc.57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

23CS101 - Problem Solving Using 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:

• Traversal: Accessing each element using a loop.


• Searching: Finding an element within the array.
• Linear Search: Traverse the array, comparing each element with the target.
• Binary Search (if the array is sorted): Repeatedly divide the search interval in
half.
• Insertion: Adding an element at a specific position.
Advantages:
• Simplicity:Easy to declare, understand, and use for basic data storage.
• Ideal for storing and accessing a list of similar items like marks, scores, or
temperatures.
• Contiguous Memory Allocation:All elements are stored in contiguous memory
locations, allowing for fast access using index arithmetic.
• This leads to efficient memory management and faster access times compared to
linked data structures.
• Ease of Traversal:Simple looping structures allow for easy traversal and
manipulation of array elements.
• Efficient Indexing:Direct access to any element using its index, providing constant
time complexity, O(1), for access and modification.
Disadvantages:

• 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:

• A 2D array is essentially an array of arrays. It is used to represent matrices or


grids.
• A two-dimensional array consists of a list of arrays- that have similar data types.
• It has a total of two dimensions.
• The parameters that receive it must define an array’s rightmost dimension.
• Total number of Bytes = The size of array visible or data type x the size of second
index x the size of the first index.
• Syntax:
Example:
Operations on 2D Arrays:

• Matrix Multiplication: Multiply two matrices element by element.

• 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:

• Complexity: The syntax and management of 2D arrays can be more complex


compared to 1D arrays, especially when dealing with dynamic 2D arrays.
• Fixed Size: Similar to 1D arrays, the size of a 2D array is static and must be defined
at compile-time.
• Dynamic resizing is more complicated than in 1D arrays and often requires
manual memory management.
• Memory Overhead: If a 2D array is sparsely populated it can lead to significant
memory wastage.
• Difficulty in Operations: Operations such as matrix multiplication, transposition,
or inversion are more complex to implement with 2D arrays and often require
nested loops, which can impact readability and performance.
1D VS 2D Array:

You might also like