Arrays in C Programming

An array is a collection of variables of the same data type stored at contiguous memory locations. Arrays allow you to manage large amounts of data efficiently by using a single variable name with an index.


Why Use Arrays?

  • Arrays simplify the management of related data.
  • They allow accessing and manipulating data using indices.
  • Useful for tasks involving large datasets, like sorting or searching.

Types of Arrays

  1. One-Dimensional Arrays
    • A linear collection of elements.
  2. Two-Dimensional Arrays
    • Used for tabular data (like a matrix).
  3. Multi-Dimensional Arrays
    • Higher-dimensional arrays for complex data structures.

1. One-Dimensional Arrays

Declaration:

data_type array_name[size];

Initialization:

int arr[5] = {10, 20, 30, 40, 50}; // Initialized with values
int arr[5]; // Declared but not initialized (values are garbage)

Accessing Elements:

  • Use the index to access elements (indices start from 0).
  • Example: arr[0] accesses the first element.

Example Program:

#include <stdio.h>

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

for (int i = 0; i < 5; i++) {
printf(“Element at index %d: %d\n”, i, arr[i]);
}

return 0;
}

Output:

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50


2. Two-Dimensional Arrays

Declaration:

data_type array_name[rows][columns];

Initialization:

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // 2 rows and 3 columns

Accessing Elements:

  • Use two indices: matrix[row][column].

Example Program:

#include <stdio.h>

int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf(“Element at [%d][%d]: %d\n”, i, j, matrix[i][j]);
}
}

return 0;
}

Output:

Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6


3. Multi-Dimensional Arrays

Declaration:

data_type array_name[size1][size2]...[sizeN];

Usage:

  • Example: int arr[2][2][3]; represents a 3-dimensional array.

Common Operations on Arrays

  1. Traversing the Array:
    • Use loops to access each element.

    Example:

    for (int i = 0; i < size; i++)
    {
    printf("%d ", arr[i]);
    }
  2. Searching an Element: Example:
    int search = 20;
    for (int i = 0; i < 5; i++)
    {
    if (arr[i] == search)
    {
    printf("Found at index: %d\n", i);
    break;
    }
    }
  3. Sorting an Array:
    • Bubble sort, selection sort, etc.

Advantages of Arrays

  1. Efficient Data Management:
    • Groups related data using a single name.
  2. Random Access:
    • Access any element directly using its index.
  3. Easier Iteration:
    • Iterate through elements using loops.

Limitations of Arrays

  1. Fixed Size:
    • Size must be specified during declaration and cannot change at runtime.
  2. Homogeneous Data:
    • Can store only one type of data.
  3. Memory Consumption:
    • Memory is allocated even if some elements are unused.

Example Program: Array Operations

#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;

// Traversing and calculating the sum
for (int i = 0; i < 5; i++) {
sum += arr[i];
}

// Display the sum
printf(“Sum of array elements: %d\n”, sum);

return 0;
}

Output:

Sum of array elements: 150


Summary of Arrays

Feature Details
Definition A collection of variables of the same type stored at contiguous memory.
Types One-dimensional, Two-dimensional, Multi-dimensional.
Operations Traversal, Searching, Sorting, Manipulation.
Advantages Simplifies data management, Random access, Easy iteration.
Limitations Fixed size, Homogeneous data, Potential memory wastage.

Leave a Comment

Your email address will not be published. Required fields are marked *