Answer the question
In order to leave comments, you need to log in
How to implement the declaration of a two-dimensional array?
How can you declare a two-dimensional array without knowing its dimensions right away? That is, for example -
int array[][];
int main()
{ array = {{1,2,3,4},{1,2,3,4}}; }
Answer the question
In order to leave comments, you need to log in
pointers:
int n, m;
int **a;
//read n
//read m
a = (int **)malloc(sizeof(int *) * n);
for (int i = 0; i < m; i++)
{
a[i] = (int *)malloc(sizeof(int) * m);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a[i][j] = 1;
}
}
std::vector< std::vector<int> > a;
int n, m;
//read n
//read m
for (int i = 0; i < n; i++)
{
std::vector<int> v(m);
for (int j = 0; j < m; j++)
{
v[j] = (i + j);
}
a.push_back(v);
}
std::cout << a[3][3] << std::endl;
Without knowing the size in advance - no way. Use dynamics (pointers), or containers that suit your task.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question