Paramclasses Academy - Blog

Tutorials Details

What is Arrays in C with Data Structures and Algorithms ?

 What is Arrays in C with Data Structures and Algorithms ?

October 17, 2026

Introduction

Arrays are one of the most fundamental data structures in C programming. They allow programmers to store multiple values of the same data type under a single variable name and access each value using an index.

Arrays are widely used in applications such as student management systems, inventory management, mathematical computations, data processing, and searching and sorting algorithms.

Understanding arrays is essential before learning more advanced data structures such as linked lists, stacks, queues, trees, and graphs.

In this module, you will learn array fundamentals, traversal, insertion, deletion, searching, updating, two-dimensional arrays, and practical array programs in C.

1. Introduction to Arrays in DSA

What Is an Array?

An array is a collection of elements of the same data type stored in contiguous memory locations and accessed using indices.

For example, if a program needs to store the marks of five students, an integer array can hold all five marks under a single variable name.

Syntax of an Array in C

data_type array_name[size];

Here:

  • data_type specifies the type of elements stored in the array.

  • array_name is the identifier used to access the array.

  • size specifies the number of elements the array can hold.

Example

int marks[5] = {85, 90, 78, 88, 92};

This declaration creates an integer array named marks with five elements.

In C, array indexing starts from zero.

Index Value
0 85
1 90
2 78
3 88
4 92

Therefore, marks[0] accesses the first element, while marks[4] accesses the fifth element.

Important Characteristics of Arrays

  • Arrays store elements of the same declared type.

  • Elements of an array occupy contiguous memory locations.

  • Array indexing begins at zero in C.

  • Accessing an element by index takes constant time, (O(1)), for ordinary array access.

  • A fixed-size array cannot automatically grow when additional elements are needed.

  • Accessing an index outside the valid array bounds results in undefined behavior in C.

Example: Declaring and Accessing an Array

#include 

int main(void) {
    int numbers[5] = {10, 20, 30, 40, 50};

    printf("First element: %d\n", numbers[0]);
    printf("Third element: %d\n", numbers[2]);
    printf("Last element: %d\n", numbers[4]);

    return 0;
}

Output:

First element: 10
Third element: 30
Last element: 50

Advantages of Arrays

  • Store multiple related values using one variable name.

  • Provide fast indexed access.

  • Make traversal and sorting straightforward.

  • Work efficiently with many algorithms.

  • Use contiguous memory, which can improve cache performance.

Limitations of Arrays

  • Fixed-size arrays have a predetermined capacity.

  • Inserting or deleting elements in the middle generally requires shifting elements.

  • In a conventional C array, all elements have the same declared type.

  • The programmer must ensure that array accesses stay within valid bounds.

2. Array Traversal

What Is Array Traversal?

Array traversal is the process of visiting each element of an array, usually to display, examine, or process its value.

A loop is commonly used to traverse an array.

Algorithm

  1. Start at index zero.

  2. Access the element at the current index.

  3. Perform the required operation.

  4. Move to the next index.

  5. Continue until the last valid element has been processed.

C Program: Array Traversal

#include 

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}

Output:

10 20 30 40 50

Explanation

The expression

sizeof(arr) / sizeof(arr[0])

calculates the number of elements when arr is an actual array in the current scope. It divides the total size of the array by the size of one element.

The loop begins at index zero and continues while i < n. This ensures that each valid element is accessed exactly once.

Time and Space Complexity

  • Time complexity: (O(n)), because every element is visited.

  • Auxiliary space complexity: (O(1)), because only a constant amount of additional memory is used.

3. Array Insertion

What Is Array Insertion?

Array insertion means adding a new element at a specified position in an array.

For a fixed-size array, insertion is possible only when sufficient capacity is available. If an element is inserted into the middle or beginning of the logical array, existing elements may need to be shifted to the right.

Suppose the array contains:

10 20 30 40

If 25 is inserted at index 2, the resulting array becomes:

10 20 25 30 40

The elements 30 and 40 move one position to the right.

Algorithm for Insertion

  1. Check whether the array has enough capacity.

  2. Validate the insertion position.

  3. Shift elements right, starting from the last existing element.

  4. Place the new value at the required index.

  5. Increase the logical size by one.

C Program: Insert an Element into an Array

#include 

int main(void) {
    int arr[10] = {10, 20, 30, 40};
    int n = 4;
    int capacity = 10;

    int position = 2;
    int value = 25;

    if (n >= capacity || position < 0 || position > n) {
        printf("Insertion not possible.\n");
        return 0;
    }

    for (int i = n; i > position; i--) {
        arr[i] = arr[i - 1];
    }

    arr[position] = value;
    n++;

    printf("Array after insertion: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}

Output:

Array after insertion: 10 20 25 30 40

Explanation

  • arr[10] provides capacity for ten integers.

  • n = 4 represents the number of currently stored elements.

  • position = 2 specifies the index at which the new value will be inserted.

  • The loop shifts elements to the right, starting from the last existing element.

  • arr[position] = value places 25 at index 2.

  • n++ updates the logical size.

In C, array positions are zero-based. An insertion position can range from 0 through n, inclusive, provided there is enough capacity.

Time Complexity

  • Best case: (O(1)) when inserting at the end, assuming capacity is available.

  • Worst case: (O(n)) when elements must be shifted.

  • Auxiliary space: (O(1)).

4. Array Deletion

What Is Array Deletion?

Array deletion means removing an element from the logical contents of an array.

When an element is deleted from the middle, subsequent elements must generally shift one position to the left to fill the gap.

Consider the array:

10 20 30 40 50

If the element at index 2 is deleted, the resulting array becomes:

10 20 40 50

Algorithm for Deletion

  1. Check whether the array contains any elements.

  2. Validate the index of the element to delete.

  3. Shift all subsequent elements one position to the left.

  4. Decrease the logical size by one.

C Program: Delete an Element from an Array

#include 

int main(void) {
    int arr[10] = {10, 20, 30, 40, 50};
    int n = 5;
    int position = 2;

    if (position < 0 || position >= n) {
        printf("Invalid deletion position.\n");
        return 0;
    }

    for (int i = position; i < n - 1; i++) {
        arr[i] = arr[i + 1];
    }

    n--;

    printf("Array after deletion: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}

Output:

Array after deletion: 10 20 40 50

Explanation

The loop copies each subsequent element into the previous position. After the shift, n-- decreases the logical size.

The old value may still physically exist in the unused portion of the allocated array, but it is no longer part of the logical array contents.

Time Complexity

  • Best case: (O(1)) when deleting the last element.

  • Worst case: (O(n)) when deleting from the beginning.

  • Auxiliary space: (O(1)).

5. Array Searching

What Is Array Searching?

Array searching is the process of finding whether a particular value exists in an array and, if it does, determining its position.

Two fundamental searching algorithms are linear search and binary search.

5.1 Linear Search

Linear search checks each element sequentially until the target is found or all elements have been examined.

It works with both sorted and unsorted arrays.

C Program: Linear Search

#include 

int main(void) {
    int arr[] = {15, 25, 35, 45, 55};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 35;
    int position = -1;

    for (int i = 0; i < n; i++) {
        if (arr[i] == target) {
            position = i;
            break;
        }
    }

    if (position != -1) {
        printf("Element found at index %d\n", position);
    } else {
        printf("Element not found\n");
    }

    return 0;
}

Output:

Element found at index 2

Complexity:

  • Best case: (O(1)).

  • Worst case: (O(n)).

  • Auxiliary space: (O(1)).

5.2 Binary Search

Binary search repeatedly divides the search interval into two halves. At each step, it compares the target with the middle element and continues in the half where the target could exist.

Important: Binary search requires the array to be sorted according to the search order.

C Program: Binary Search

#include 

int main(void) {
    int arr[] = {10, 20, 30, 40, 50, 60, 70};
    int n = sizeof(arr) / sizeof(arr[0]);

    int target = 50;
    int low = 0;
    int high = n - 1;
    int position = -1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (arr[mid] == target) {
            position = mid;
            break;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    if (position != -1) {
        printf("Element found at index %d\n", position);
    } else {
        printf("Element not found\n");
    }

    return 0;
}

Output:

Element found at index 4

Linear Search vs Binary Search

Feature Linear Search Binary Search
Sorted array required No Yes
Worst-case time (O(n)) (O(\log n))
Auxiliary space, iterative (O(1)) (O(1))
Method Checks elements sequentially Repeatedly halves the search interval
Suitable for Small or unsorted arrays Sorted arrays

Binary search is generally more efficient for large sorted arrays. However, sorting an array solely to perform one search may not be worthwhile if the sorting cost outweighs the benefit.

6. Array Updating

What Is Array Updating?

Array updating means changing the value stored at a particular index without changing the number of elements in the array.

For example, if the array contains:

10 20 30 40 50

Updating the value at index 2 to 99 produces:

10 20 99 40 50

C Program: Update an Array Element

#include 

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    int position = 2;
    int newValue = 99;

    if (position < 0 || position >= n) {
        printf("Invalid array index.\n");
        return 0;
    }

    arr[position] = newValue;

    printf("Updated array: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}

Output:

Updated array: 10 20 99 40 50

Explanation

The statement arr[position] = newValue replaces the value at the selected index.

Unlike insertion and deletion, updating an existing element does not require shifting other elements.

Time Complexity

  • Updating by a known index: (O(1)).

  • Auxiliary space: (O(1)).

If the value must first be found using a search, the total time also includes the cost of searching.

7. Two-Dimensional Arrays

What Is a Two-Dimensional Array?

A two-dimensional array organizes data into rows and columns. It is useful for representing matrices, tables, grids, and other rectangular data sets.

Syntax

data_type array_name[rows][columns];

For example:

int matrix[2][3] = {
    {10, 20, 30},
    {40, 50, 60}
};

This array contains two rows and three columns.

Row / Column Column 0 Column 1 Column 2
Row 0 10 20 30
Row 1 40 50 60

In C, multidimensional arrays are stored in row-major order. The elements of the first row are followed by the elements of the second row in memory.

C Program: Display a Two-Dimensional Array

#include 

int main(void) {
    int matrix[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Output:

10 20 30
40 50 60

Explanation

  • The outer loop iterates through the rows.

  • The inner loop iterates through the columns of the current row.

  • matrix[i][j] accesses the element at row i and column j.

For an array with r rows and c columns, traversing every element takes (O(rc)) time.

Practical Application: Matrix Addition

Two matrices of the same dimensions can be added by adding their corresponding elements.

#include 

int main(void) {
    int a[2][2] = {
        {1, 2},
        {3, 4}
    };

    int b[2][2] = {
        {5, 6},
        {7, 8}
    };

    int sum[2][2];

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("Sum of matrices:\n");

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", sum[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Output:

Sum of matrices:
6 8
10 12

The program performs one addition per matrix element.

For matrices with r rows and c columns:

  • Time complexity: (O(rc)).

  • Auxiliary space for the result matrix: (O(rc)).

8. Practical Array Programs

The following programs demonstrate common operations performed with arrays in C.

Program 1: Find the Sum and Average of Array Elements

#include 

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    double average = (double)sum / n;

    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", average);

    return 0;
}

Output:

Sum = 150
Average = 30.00

Complexity: Time (O(n)), auxiliary space (O(1)).

Program 2: Find the Largest Element

#include 

int main(void) {
    int arr[] = {45, 12, 78, 34, 90, 23};
    int n = sizeof(arr) / sizeof(arr[0]);

    int largest = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] > largest) {
            largest = arr[i];
        }
    }

    printf("Largest element = %d\n", largest);

    return 0;
}

Output:

Largest element = 90

Complexity: Time (O(n)), auxiliary space (O(1)).

This program assumes the array contains at least one element.

Program 3: Reverse an Array

#include 

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    int left = 0;
    int right = n - 1;

    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;

        left++;
        right--;
    }

    printf("Reversed array: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}

Output:

Reversed array: 50 40 30 20 10

Complexity: Time (O(n)), auxiliary space (O(1)).

The program swaps corresponding elements from opposite ends and moves toward the center.

Program 4: Count Even and Odd Elements

#include 

int main(void) {
    int arr[] = {12, 7, 8, 15, 20, 9};
    int n = sizeof(arr) / sizeof(arr[0]);

    int even = 0;
    int odd = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0) {
            even++;
        } else {
            odd++;
        }
    }

    printf("Even elements = %d\n", even);
    printf("Odd elements = %d\n", odd);

    return 0;
}

Output:

Even elements = 3
Odd elements = 3

Complexity: Time (O(n)), auxiliary space (O(1)).

Program 5: Copy One Array into Another

#include 

int main(void) {
    int source[] = {10, 20, 30, 40, 50};
    int n = sizeof(source) / sizeof(source[0]);

    int destination[5];

    for (int i = 0; i < n; i++) {
        destination[i] = source[i];
    }

    printf("Copied array: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", destination[i]);
    }

    printf("\n");
    return 0;
}

Output:

Copied array: 10 20 30 40 50

Complexity: Time (O(n)), auxiliary space (O(n)) for the destination array.

Common Array Programming Mistakes

When working with arrays in C, remember the following points:

  1. Incorrect indexing: For an array of size n, valid indices range from 0 to n - 1.

  2. Out-of-bounds access: Accessing elements outside the valid range results in undefined behavior.

  3. Incorrect logical size: When implementing insertion and deletion, update the number of active elements correctly.

  4. Missing capacity checks: Never insert into a full fixed-capacity array.

  5. Incorrect binary search assumptions: Binary search requires sorted input.

  6. Incorrect loop conditions: Carefully distinguish between i < n and i <= n.

  7. Uninitialized values: Reading uninitialized local array elements can produce undefined behavior.

Following these practices makes array-based programs more reliable and easier to maintain.

Conclusion

Arrays are a foundational data structure in C and DSA. They provide contiguous storage, efficient indexed access, and a convenient way to process collections of related values.

In this module, we covered:

  • Introduction to arrays and their characteristics

  • Array traversal

  • Array insertion

  • Array deletion

  • Linear and binary searching

  • Updating array elements

  • Two-dimensional arrays and matrix addition

  • Practical programs involving sums, averages, maximum values, reversal, counting, and copying

Understanding these operations will help you analyze the trade-offs between different data structures and prepare for more advanced topics.