how to create a 2D array

hey guys
can someone give me an example of a 2D array because i need to use to in my assignment and i just found out that its not the same case as an array with one variable. how can i initialize it? how can i use it?
thanks in advance :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
int main()
{
    const size_t M = 10;
    const size_t N = 20;
    int a[M][N] = {};
    
    for ( const auto &row : a )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }
    
    return 0;
}
thank you :) can you please explain to me what you did in line 9 and line 11? what is const auto used for? what does " int x : row " do? thanks again
Well it is so-called the for statement based on range. You can simplify the code if you do not know what is the for statement based on range

1
2
3
4
5
for ( size_t i = 0; i < M; i++ )
{
   for ( size_t j = 0; j < N; j++ ) std::cout << a[i][j] << ' ';
   std::cout << std::endl;
}
thank you a lot :)
Topic archived. No new replies allowed.