Articles / DSA

Limitations and Applications of Linear Arrays

Understand where linear arrays are useful, their limitations, and when other data structures may be a better choice.

Limitations and Applications of Linear Arrays

In the previous articles, we learned how arrays store elements, how to access them, and how to perform basic operations.

Arrays are useful because they provide direct access to elements using indexes. However, they also have some limitations.

Let’s understand where arrays are useful and where they can become difficult to work with.

1. Limitations of Linear Arrays

A. Fixed Size

In C, when we declare an ordinary array with a fixed size, its capacity is decided at declaration.

For example:

int marks[5] = {70, 80, 90, 85, 75};

This array can hold five elements.

If we later need to store 10 marks, we cannot simply increase the size of this existing array. We need another array with a larger capacity and must transfer the existing elements.

B. Insertion and Deletion Can Be Costly

Adding or removing elements from the middle of an array may require shifting other elements.

For example, inserting 50 at index 1:

Before:  10   20   30   40

After:   10   50   20   30   40

The existing elements from index 1 onward must move one position to the right.

Similarly, deleting an element may require shifting the remaining elements to fill the empty position.

Therefore, insertion and deletion in the middle of an array can require considerable work.

C. Continuous Memory Requirement

Array elements are stored in contiguous memory locations.
This means the computer needs a sufficiently large continuous block of memory for the array.

For very large arrays, finding such a block can sometimes be difficult, even when the computer has enough total free memory.

D. Stores Elements of the Same Data Type

An ordinary C array stores elements of the same data type.

For example:

int numbers[4] = {10, 20, 30, 40};

This array stores integers. We cannot directly place a string or another unrelated data type into one of its elements.

If we need to store different types of information together, we can use structures or other suitable data structures.

2. Applications of Linear Arrays

Despite these limitations, arrays are widely used in programming.

A. Storing Lists of Data

Arrays are useful when we need to store a collection of similar values.

For example, we can store:

  • Marks of students.
  • Daily temperatures.
  • Product prices.
  • Employee IDs.
int temperatures[5] = {22, 24, 21, 26, 23};

All five values can be accessed using one array name.

B. Searching and Sorting

Arrays are commonly used with searching and sorting algorithms.

For example, we can search for a particular number or arrange elements in ascending order.

In upcoming articles, we’ll study algorithms such as linear search, binary search, bubble sort, selection sort, and insertion sort.

C. Representing Matrices and Tables

Two-dimensional arrays allow us to represent information in rows and columns.

They are useful for:

  • Mathematical matrices.
  • Student marksheets.
  • Game boards.
  • Tabular data.

D. Temporary Data Storage

Arrays are useful when a program needs to store a collection of values temporarily while performing calculations or processing information.

For example, a program can store the marks of students, calculate their average, and find the highest mark.

3. When Should We Use Arrays?

Arrays are a good choice when:

  • We know approximately how many elements we need to store.
  • We frequently access elements using their indexes.
  • We want to store multiple values of the same data type.
  • We need a simple way to organise and process a collection of data.

However, if a program frequently inserts or deletes elements, or needs to grow its collection dynamically, another data structure may be more suitable.

We’ll explore such alternatives when we study linked lists.

Arrays provide a simple and efficient way to store and access collections of data. However, fixed capacity in ordinary C arrays, shifting during insertion and deletion, contiguous memory requirements, and restrictions on element types can make them unsuitable for certain situations.

Understanding both their advantages and limitations helps us choose the right data structure for a problem.

In the next article, we’ll explore Strings: Understanding String Data.

Keep a copy

Print this article or save it as a PDF to read offline.

farhanpaal.com

Keep reading

DSA

Types of Data Structures: A Complete Overview

Learn about the different types of data structures, including primitive, non-primitive, linear, non-linear, static, and dynamic structures, with simple examples and practical…

14 min read

Ask Farhan

Ask anything about Farhan