An array is a collection of similar types of elements. For example, an integer array holds the elements of int types while a character array holds the elements of char types. Below is the representation of the array:
Though, array got its own set of advantages and disadvantages.
Advantages of Arrays
Below are some advantages of the array:
- In an array, accessing an element is very easy by using the index number.
- The search process can be applied to an array easily.
- 2D Array is used to represent matrices.
- For any reason a user wishes to store multiple values of similar type then the Array can be used and utilized efficiently.
Disadvantages of Arrays
Now let’s see some disadvantages of the array and how to overcome it:
Array size is fixed: The array is static, which means its size is always fixed. The memory which is allocated to it cannot be increased or decreased. Below is the program for the same:
Element at index 0 is 5 Element at index 11 is -1176897384
Explanation: In the above program the array of size 10 is declared and the value is assigned at a particular index. But when the value at index 11 is printed then it prints the garbage value because the array was accessed out of the bound index. In some compiler, it gives error as “Array Index Out Of Bound.”.
How to overcome: To overcome that problem use Dynamic Memory Allocation like malloc(), calloc(). It also helps us to deallocates the memory using the free() method which helps to reduce wastage of memory by releasing it. Below is the program for the same:
The elements are: 1 2 3 4 5 6 7 8 9 10
Array is homogeneous:The array is homogeneous, i.e., only one type of value can be store in the array. For example, if an array type “int“, can only store integer elements and cannot allow the elements of other types such as double, float, char so on. Below is the program for the same:
100 547.000000 Ram
Explanation: The above code gives “Compilation Error” as an integer type array is assigned value to a string and float type.
How to overcome: To overcome that problem, the idea is to structure, where it can store non-homogeneous (heterogeneous) value. Below is the program for the same:
100 547.000000 Ram
Array is Contiguous blocks of memory: The array stores data in contiguous(one by one) memory location. Below is the representation of the same:
How to overcome: To overcome the sequential access to the array, the idea is to use the Linked list. In a Linked list, the elements are not stored in contiguous memory locations. Below is the representation of the same:
Insertion and deletion are not easy in Array: The operation insertion and deletion over an array are problematic as to insert or delete anywhere in the array, it is necessary to traverse the array and then shift the remaining elements as per the operation. This operation cost is more.
Example: For inserting 22 in 3rd position of the array then below are the steps:
Below is the program to illustrate the same:
1 2 3 4 5 6 7 8 9 10 Array after inserting 50 at position 5 1 2 3 4 50 5 6 7 8 9 10
How to overcome: To overcome the above problem using a Linked List. Below is the representation of the same:
Below is the program to implement the same:
Current Linked List is: 3 1 5 7 Linked List after insert 6 in 4th position: 3 1 5 6 7
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Structured Programming Approach with Advantages and Disadvantages
- Advantages, Disadvantages, and uses of Doubly Linked List
- Advantages of vector over array in C++
- What’s difference between “array” and “&array” for “int array” ?
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Find original array from encrypted array (An array of sums of other elements)
- Find Array formed by adding each element of given array with largest element in new array to its left
- Array obtained by repeatedly reversing array after every insertion from given array
- Find an element in array such that sum of left array is equal to sum of right array
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Reduce array to longest sorted array possible by removing either half of given array in each operation
- Difference between pointer to an array and array of pointers
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Queries to search for an element in an array and modify the array based on given conditions
- Generate array having differences between count of occurrences of every array element on its left and right
- Difference between Programmable Logic Array and Programming Array Logic
- Rearrange array by interchanging positions of even and odd elements in the given array
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.