-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,333
  • 19
  • 88
  • 134

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
  • 22,732
  • 9
  • 48
  • 60
1
int * * mat = new int * [ n ];
kotlomoy
  • 1,390
  • 7
  • 13