Are you preparing to appear for GATE CSE exams?
Data Structures is one of the most important topics from which you will be asked to answer a lot of questions.
Understanding the array data structure for GATE CSE
Let’s see the main Array concepts you should cover for answering the GATE questions on data structures.
Arrays
If you have a collection of the same type of elements, and also with the same data type, you could use an array to store them. Then, whenever you want to access the stored elements, you can call them using the same name. The characteristic feature of the memory locations occupied by an array is that they are contiguous in nature. In C-programming language, the index of an array starts at ‘0’ and the final position of the index is mentioned as one minus the total size of the array.
Why do we use an array?
Consider this scenario where you have five numbers of data type – integer. You may want to store it somewhere to facilitate further processing. Here, all the elements belong to the same data type. The size of the collection under consideration is fixed. In such cases, you will use an array to store the values. An array is a data structure provided by all programming languages.
Do you know how an array works?
Arrays are container objects. They store a collection of values belonging to the same data type. The collection should have a fixed size as well. At the time of the creation itself, the length of an array has to be declared. Each item stored in an array will have an index value. The elements stored in an array are referred to to their index values. In your program when there is a line calling an array value has reached, the value stored in the index mentioned in your line of code will be retrieved from a contiguous memory location and the value will be further processed.
While attending the GATE questions on data structures you cannot always expect direct questions. You have to be familiar with why and where a concept is being used.
Let’s see the advantages, disadvantages and the programming applications of the array data structure.
What are the advantages of using an array?
- You can store multiple elements in one place
- You can refer to values with the same object name
- The stored elements can be accessed in any order by referring to their index values
- Arrays use contiguous memory for efficient memory usage
- It avoids wastage allocating extra memory
- It avoids memory overflow
- It avoids memory shortage in the arrays themselves
- Arrays can form the basis of other data structure types
- Arrays have 2D capability making them perfect to implement a matrix
What are the disadvantages of using an array?
- The size of an array is fixed
- You have to know how many elements you are going to store in an array beforehand
- You have to declare the size of the array at the time of its creation itself
- Once created, the size of an array can never be changed
- As there is this risk of under-allocation of memory the programmers tend to allocate buffer memory which leads to wastage of available memory
- The allocated memory of an array can never be adjusted further
- If you have plans to perform insertion and deletion operations on an array it will be quite tough considering the consecutive nature of the array memory
What are the programming applications of arrays?
When you have to deal with large data, arrays let us store it under a single name. Arrays also give us the flexibility to address the data elements using the same name with reference to the index values. If we hadn’t had arrays, we should have been using multiple variables to store and work with large data sets. It could have been really difficult for programmers to work with bigger data for bigger applications.
While developing your application, if you ever come across the requirement to implement sorting functionality, Arrays will be handy for you. You can easily implement various types of sorting functionality.
If you are dealing with the database records of two-dimensional nature, Arrays will be useful for executing matrix operations. If the application you develop involves certain systemic functions like CPU rescheduling, Arrays can be used to implement that functionality.
Example of an array