How to store array inside array in c++?

Member

by viviane , in category: C/C++ , 2 years ago

How to store array inside array in c++?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by flossie.kessler , a year ago

@viviane 

In C++, you can store an array inside another array by creating a two-dimensional array. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
const int ROWS = 3;
const int COLS = 4;
int matrix[ROWS][COLS];

// initialize the matrix with some values
for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
        matrix[i][j] = i * COLS + j;
    }
}

// print the matrix
for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
        std::cout << matrix[i][j] << " ";
    }
    std::cout << std::endl;
}


In this example, we create a 2-dimensional array called matrix with 3 rows and 4 columns. We then initialize the matrix with some values and print it out. You can access individual elements of the matrix using the two indices, like matrix[i][j].


You can also create arrays of arrays (i.e., arrays of pointers to arrays) using dynamic memory allocation. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const int ROWS = 3;
const int COLS = 4;

// create an array of pointers to arrays
int** matrix = new int*[ROWS];

// create each row array and assign it to the corresponding pointer
for (int i = 0; i < ROWS; i++) {
    matrix[i] = new int[COLS];
}

// initialize the matrix with some values
for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
        matrix[i][j] = i * COLS + j;
    }
}

// print the matrix
for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
        std::cout << matrix[i][j] << " ";
    }
    std::cout << std::endl;
}

// free the memory
for (int i = 0; i < ROWS; i++) {
    delete[] matrix[i];
}
delete[] matrix;


In this example, we first create an array of pointers to arrays called matrix, with 3 elements. We then create each row array using new and assign it to the corresponding pointer. We initialize the matrix with some values, print it out, and then free the memory using delete[]. Note that when using dynamic memory allocation, you need to manually free the memory to avoid memory leaks.

by eda.wolff , 9 months ago

@viviane 

In C++, you can store an array inside another array using a two-dimensional array or a vector of vectors.

  1. Two-dimensional array: Declare a 2D array with a fixed size and store arrays of a specific type inside it. Here's an example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const int ROWS = 3;
const int COLS = 4;

int main() {
    int arr[ROWS][COLS] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Accessing elements
    cout << arr[0][2] << endl; // Output: 3

    return 0;
}


  1. Vector of vectors: Using vectors allows dynamic sizing. Declare a vector and store vectors of a specific type inside it. Here's an example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include 
#include 
using namespace std;

int main() {
    vector> arr = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Accessing elements
    cout << arr[1][2] << endl; // Output: 6

    // Modifying elements
    arr[0][1] = 10;
    cout << arr[0][1] << endl; // Output: 10

    return 0;
}


Both methods provide ways to store arrays inside arrays, but a vector of vectors offers more flexibility with dynamic sizing.