- arrays are the simplest data structure that one encounters when one starts a course in any language and study dsa.
- ok, let me give you a brief of the roadmap of dsa so that you get familiar with the sequencing of the dsa course that exactly where arrays lie. While studying any language like c++, c , python , java etc one gets to study the following:
- basic language
- arrays and strings
- linked list
- trees
- graphs
- more advanced algorithms
-
arrays can be visualised as a continuos box of containers. these containers can have elements inside them.
-
these containers are situated side by side i.e we can say that arrays have contiguous memory location.
-
the elements present in the array are of the same type and that will depend on how we have declared our array.
-
for example : int arr[10]
This means that we have declared an array which has name="arr" and size ="10" and the elements stored in the array will be integers only
this can be visualised as a single row of containers placed side by side
this can be visualised as if containers are present along the row and column
this can be visualised as if containers are present along the length,breadth and height of the array.
- declaring the array
- inserting elements in the array(at first , at last , at random position)
- deleting the elements in the array(at first , at last and at the random position)
- sorting the elements in the array
- performing linear and binary search in the array
- finding palindromes , duplicate number , maximum element , minimum element
- finding prime , composite , even , armstrong etc.
These are the basic programs for making array's fundamentals to be crystal clear for you.
Problem with arrays
-
Arrays have a fixed size, making them less flexible for handling dynamic or varying data.
-
Arrays require manual memory management, increasing the risk of errors like buffer overflows or memory leaks. So we cannot delete element from the array without writing the extra code for managing the size of the array.
-
Arrays lack inherent size information, leading to potential bugs in tracking and managing array size.
Is there any way forward ? vector is a dynamic array implementation in C++. It is part of the Standard Template Library (STL) and provides a flexible and efficient alternative to static arrays
-
Dynamic Resizing: Vectors automatically resize themselves, eliminating the need for manual memory management. Arrays have a fixed size that needs to be declared beforehand.
-
Ease of Use: Vectors provide convenient member functions like push_back() and pop_back() for adding and removing elements, making them more user-friendly.
Arrays are fundamental data structures in programming. We have to understand them well so as to move ahead in the roadmap of dsa i provided at the begining of this documentation.