-5

I want to create a matrix of n*1 (a matrix of one column. n can be any integer)

I think It should be something like:

int mat[][1];
cin >> n;
*mat = new int[n]*;

any help appreciated!

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

2 Answers2

3

If you declare your matrix as:

int mat[][1];

It means that you are not doing dynamic memory allocation.

You should do the following:

int **mat = new int*[n]; //n is number of rows
for (int i = 0; i < n ;++i)
{
   mat[i] = new int[1];
}

Anyway, you should prefer to use std::vector instead of using dynamic allocated arrays, especially when you have only 1 column.

taocp
  • 23,276
  • 10
  • 49
  • 62
1
int * * mat = new int * [ n ];
kotlomoy
  • 1,420
  • 8
  • 14