๐ŸŒŸ Array in Programming: Concepts, Types, Declaration.

AdityaAditya
2 min read

Arrays are one of the most fundamental and essential concepts in programming. Whether you're coding in C, C++, Java, or Python, arrays play a crucial role in data storage and manipulation.


๐Ÿ“Œ What is an Array?

An array is a collection of similar data types stored in contiguous memory locations. It allows you to store multiple values in a single variable instead of declaring separate variables for each value.

๐Ÿ”น Key Features:

  • Fixed size

  • Stores elements of the same data type

  • Indexed (starts from index 0)

  • Efficient for iteration and data manipulation


๐Ÿงฉ Types of Arrays

1. One-Dimensional Array (1D)

A linear array where data is stored in a single row (like a list).

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

2. Two-Dimensional Array (2D)

Used for tabular data, like matrices or grids.

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

3. Multidimensional Array (3D and more)

Arrays of arrays of arrays โ€” used in complex applications like 3D graphics.

int space[2][2][2] = {
    {{1,2}, {3,4}},
    {{5,6}, {7,8}}
};

๐Ÿ› ๏ธ Array Declaration and Initialization

โœ… Declaration

Syntax (C/C++):

data_type array_name[size];

Example:

int numbers[10]; // Declares an integer array of size 10

โœ… Initialization

There are 3 ways:

๐Ÿ”น Method 1: At declaration time

int a[5] = {1, 2, 3, 4, 5};

๐Ÿ”น Method 2: Partial initialization

int b[5] = {10, 20}; // Remaining values are set to 0

๐Ÿ”น Method 3: Index-based assignment

int c[5];
c[0] = 11;
c[1] = 22;
c[2] = 33;

๐Ÿ’ก Example: Sum of Array Elements

#include <iostream>
using namespace std;

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

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

    cout << "Sum = " << sum;
    return 0;
}

๐Ÿ’ฅ Output:

Sum = 150

๐ŸŽฏ Conclusion

Arrays are a powerful structure for storing and manipulating data in programming. By understanding their types, declaration methods, and usage patterns, you can build efficient and organized code. Arrays form the base of many advanced data structures such as stacks, queues, and matrices.

1
Subscribe to my newsletter

Read articles from Aditya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya
Aditya