Skip to content

One Dimensional Array and Multi-Dimensional Arrays

One Dimensional Array and Multi-Dimensional Arrays

Arrays can be categorized based on their dimensions. The most common are one-dimensional arrays and multi-dimensional arrays, such as two-dimensional and three-dimensional arrays.

One-Dimensional Arrays

A one-dimensional array (often called a single-dimensional array) is the simplest form of an array. It is essentially a list of elements, all of the same type, stored in contiguous memory locations.

Declaration and Initialization

In C++:

// Declaration

int arr[5];

// Initialization

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

// Accessing elements

cout << arr[0]; // Outputs: 1

arr[2] = 10; // Modifies the third element to 10

In Python:

# Declaration and initialization

arr = [1, 2, 3, 4, 5]

# Accessing elements

print(arr[0]) # Outputs: 1

arr[2] = 10 # Modifies the third element to 10

Traversing a One-Dimensional Array

In C++:

#include <iostream>

using namespace std;

int main() {

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

    // Traversing the array

    for (int i = 0; i < 5; i++) {

        cout << arr[i] << ” “;

    }

    cout << endl;

    return 0;

}

In Python:

arr = [1, 2, 3, 4, 5]

# Traversing the array

for element in arr:

    print(element, end=” “)

print()

Multi-Dimensional Arrays

Multi-dimensional arrays are arrays of arrays. The most common multi-dimensional arrays are two-dimensional arrays (matrices), but arrays can have any number of dimensions.

Two-Dimensional Arrays

A two-dimensional array is like a table or matrix where each element is identified by two indices: a row index and a column index.

Declaration and Initialization

In C++:

// Declaration

int arr[3][4];

// Initialization

int arr[3][4] = {

    {1, 2, 3, 4},

    {5, 6, 7, 8},

    {9, 10, 11, 12}

};

// Accessing elements

cout << arr[0][1]; // Outputs: 2

arr[2][3] = 15; // Modifies the element at the third row and fourth column to 15

In Python:

# Declaration and initialization

arr = [

    [1, 2, 3, 4],

    [5, 6, 7, 8],

    [9, 10, 11, 12]

]

# Accessing elements

print(arr[0][1]) # Outputs: 2

arr[2][3] = 15 # Modifies the element at the third row and fourth column to 15

Traversing a Two-Dimensional Array

In C++:

#include <iostream>

using namespace std;

int main() {

    int arr[3][4] = {

        {1, 2, 3, 4},

        {5, 6, 7, 8},

        {9, 10, 11, 12}

    };

    // Traversing the array

    for (int i = 0; i < 3; i++) {

        for (int j = 0; j < 4; j++) {

            cout << arr[i][j] << ” “;

        }

        cout << endl;

    }

    return 0;

}

In Python:

arr = [

    [1, 2, 3, 4],

    [5, 6, 7, 8],

    [9, 10, 11, 12]

]

# Traversing the array

for row in arr:

    for element in row:

        print(element, end=” “)

    print()

Three-Dimensional Arrays

A three-dimensional array is an array of arrays of arrays. It can be visualized as a cube of elements.

Declaration and Initialization

In C++:

// Declaration

int arr[2][3][4];

// Initialization

int arr[2][3][4] = {

    {

        {1, 2, 3, 4},

        {5, 6, 7, 8},

        {9, 10, 11, 12}

    },

    {

        {13, 14, 15, 16},

        {17, 18, 19, 20},

        {21, 22, 23, 24}

    }

};

// Accessing elements

cout << arr[1][2][3]; // Outputs: 24

arr[0][1][2] = 10; // Modifies the element at the first slab, second row, and third column to 10

In Python:

# Declaration and initialization

arr = [

    [

        [1, 2, 3, 4],

        [5, 6, 7, 8],

        [9, 10, 11, 12]

    ],

    [

        [13, 14, 15, 16],

        [17, 18, 19, 20],

        [21, 22, 23, 24]

    ]

]

# Accessing elements

print(arr[1][2][3]) # Outputs: 24

arr[0][1][2] = 10 # Modifies the element at the first slab, second row, and third column to 10

Traversing a Three-Dimensional Array

In C++:

#include <iostream>

using namespace std;

int main() {

    int arr[2][3][4] = {

        {

            {1, 2, 3, 4},

            {5, 6, 7, 8},

            {9, 10, 11, 12}

        },

        {

            {13, 14, 15, 16},

            {17, 18, 19, 20},

            {21, 22, 23, 24}

        }

    };

    // Traversing the array

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < 3; j++) {

            for (int k = 0; k < 4; k++) {

                cout << arr[i][j][k] << ” “;

            }

            cout << endl;

        }

        cout << endl;

    }

    return 0;

}

In Python:

arr = [

    [

        [1, 2, 3, 4],

        [5, 6, 7, 8],

        [9, 10, 11, 12]

    ],

    [

        [13, 14, 15, 16],

        [17, 18, 19, 20],

        [21, 22, 23, 24]

    ]

]

# Traversing the array

for slab in arr:

    for row in slab:

        for element in row:

            print(element, end=” “)

        print()

    print()

Summary

Arrays are fundamental data structures that store elements of the same type in contiguous memory locations. One-dimensional arrays represent a list of elements, while multi-dimensional arrays, such as two-dimensional arrays (matrices) and three-dimensional arrays, represent more complex structures like tables and cubes. Understanding how to declare, initialize, access, and traverse these arrays is essential for effective programming and data manipulation.