filling 2d array with 0 c++

To fill a 2D array with 0 in C++, you can follow these steps:

  1. Declare the 2D array: Start by declaring a 2D array with the desired number of rows and columns. For example, to create a 2D array with 3 rows and 4 columns, you can use the following code:
int array[3][4];
  1. Initialize the array elements: Use nested loops to iterate over each element of the array and assign 0 to each element. The outer loop will iterate over the rows, and the inner loop will iterate over the columns. For example:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        array[i][j] = 0;
    }
}
  1. Accessing the array elements: You can access the elements of the array using the row and column indices. For example, to access the element at row 1 and column 2, you can use the following code:
int element = array[1][2];

Note: In C++, arrays are zero-indexed, meaning the first element is accessed using index 0.

  1. Printing the array: To verify that the array is filled with 0, you can print the elements of the array. Use nested loops to iterate over each element and print its value. For example:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        cout << array[i][j] << " ";
    }
    cout << endl;
}

This code will print the elements of the array row by row, separated by spaces.

That's it! Following these steps will allow you to fill a 2D array with 0 in C++. Let me know if you have any further questions.