2-Dimensional array in c++

To create a 2-dimensional array in C++, you can follow these steps:

  1. Declare the array: Start by declaring the array with the desired number of rows and columns. For example, to create a 2-dimensional array with 3 rows and 4 columns, you would write:

cpp int array[3][4];

This declares an array named "array" with 3 rows and 4 columns, where each element is of type int.

  1. Initialize the array (optional): You can optionally initialize the array elements at the time of declaration. For example, to initialize all elements to 0, you can use the following syntax:

cpp int array[3][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};

This initializes each element in the array to 0.

  1. Accessing elements: You can access individual elements of the 2-dimensional array using the row and column indices. The indices start from 0, so to access the element in the second row and third column, you would use:

cpp int element = array[1][2];

This assigns the value of the element at row 1, column 2 to the variable element.

  1. Modifying elements: You can modify the value of individual elements in the 2-dimensional array using the row and column indices. For example, to change the value of the element in the third row and fourth column to 5, you would write:

cpp array[2][3] = 5;

This changes the value of the element at row 2, column 3 to 5.

Remember that the size of the array is fixed at compile-time, so you cannot change the number of rows or columns once the array is declared.