Question: So, I need to create a square matrix with a size entered by user (also filled with random numbers).
I tried this:

```
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int size;
cin >> size;
int arr[] = {};
for (int i = 0; i < size; i++){
    cout << endl;
    for (int j = 0; j < size; j++){
        arr[i][j] = rand() % 10;
        cout << arr[i][j] << ' ';

    }
} 
return 0;
}

```

However, every time my g++ output is :

```
6.cpp: In function ‘int main()’:
6.cpp:12:21: error: invalid types ‘int[int]’ for array subscript
         arr[i][j] = rand() % 10;
                 ^
6.cpp:13:29: error: invalid types ‘int[int]’ for array subscript
         cout << arr[i][j] << ' ';

```

Where have I messed up?

Answer: 
First off you are using a 1 dimensional array like a 2 dimensional one. This is wrong.

Assuming you fix that,
If this had been C99 and not C++, variable length arrays are actually okay.
You can get away with something like

```
/*
   get input size
*/
int arr[size][size];

```

But in C++ (un)fortunately there are no variable length arrays in the same style. 

In classic C++ you would have had to use memory allocation to create dynamic arrays like this with sizes that are determined at runtime

```
int size;
cin >> size;
int **arr = new int*[size];
for(int i = 0; i < size; i++) {
    arr[i] = new int[size];
}

```

However in more modern C++ SO typically advises you to use vectors instead

`vector< vector< int> > arr;`

and instead of `arr[i][j] = rand() % 10;` use `arr[i].push_back(rand() %10)`

or you could `resize` before putting stuff the vectors. OR you could use `reserve` if you don't want to put placeholder junk in the vector.

OR you could pass `size` to the vector's constructor

`vector< vector < int > > arr(size, vector < int > (size));` 

A caveat though, for small matrices vector of vectors has noticeably worse performance than plain old array. (I did not personally measure the metrics, but I have seen reliable metric for this on SO)
```
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int main(){
int size;
cin >> size;

std::vector<std::vector<int> > arr(size,std::vector<int>(size,0));
for (int i = 0; i < size; i++){
    cout << endl;
    for (int j = 0; j < size; j++){
        arr[i][j] = rand() % 10;
        cout << arr[i][j] << ' ';

    }
} 
return 0;
}

```