I searched a lot how to allocate dynamic 2D array of variable lengths(row and col)
I found these two ways:
1st one:
int (*array) [M] = new int[N][M];
2nd one:
int ** array = new int[row];
for(int i =0; i<width;i++){
array[i] = new array[col];
}
I got confused and don't know what is the difference and which is better.
Related
So,far after doing my own research i know bool** matrix will create 2d array,bool* matrix will create 1d array. Now,when implementing graphs using adjacency matrix
private:
bool** adjMatrix;
int numVertices;
public:
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];//<---here
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];//<--here
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
}
}
Now, i commented the part where i am confused, see both lines look similar to me as per my above mentioned understanding.
new bool*[numVertices] allocates and constructs an array of bool* (pointer to bool) of length numVertices. new bool[numVertices] allocates and constructs an array of bool of length numVertices.
What your code is doing is:
Declaring adjMatrix as a pointer to a pointer to bool
Setting adjMatrix to point to the first element of an array of bool*
Setting each element of that array to point to the first element of an array of bool
This gives the effect of adjMatrix looking like a 2D array, since you can write adjMatrix[i][j] and access one of the bool elements. However, this setup is not ideal for a few reasons.
It takes two pointer derefs to reach an array element rather than one
You have to do numVertices+1 dynamic allocations
The calls to new bool[numVertices] are not guaranteed to put all of the bools next to each other in memory
It would be better to make adjMatrix just be a bool*, and allocate all the bools at once:
adjMatrix = new bool[numVertices * numVertices];
This will save you numVertices dynamic allocations and put all the bools in a contiguous block of memory (better for cache). You could then access the array elements like
*(adjMatrix + i*numVertices + j)
instead of
adjMatrix[i][j]
There is a similar topic about this.
How do I declare a 2d array in C++ using new?
What I want to do is to create multiple 2d arrays according to some integer which determines how many 2d arrays there should be.
I want to create a single dimensional array first for pointers and assing every pointer to a multidimensional array, using new. But it seems like you can't ask for memory to create multidimensional array. Why can't we just write:
int** howManyPointers = new int*[translate];
for (int i = 0; i < translate; i++){
howManyPointers[i] = new char[rowsUsed][2000];
}
In my project, 2d array must have 2000 columns but row size is undetermined first. It will be given by the user. Assume you've already got it [rowsUsed]
So what?
You allocate array of pointers, and then for each pointer you allocate 1d array like this:
int** 2dArray = new int*[rows];
for (int i = 0; i < rows; ++i) {
2dArray[i] = new int[cols];
}
and then you can do 2dArray[X][Y] for each X < rows and Y < cols;
i want to create a 2d array dynamically in c++ language. But in that 2d array columns should be of different size. i mean to say that 2d array should not be in M * N.
It should be something like....
1 2 next line
3 4 5 next line
2 3 4 5 next line
5 next line
4 5 7
I am able to create 2d array in above manner but how to display content of array continously create a problem for me.
Please anyone explain me how to come up with this problem.
The best way to do this will be by using vectors. They are resizable arrays which handle all the memory management automatically. In this case, you can create a 2-D vector.
However, if for some reason you do not want to use vectors and want to use C-style arrays, then you can do it by creating an array of pointers and allocating different amounts of memory to each one of them. For storing their size, we can follow the strategy of allocating an additional cell in every array which will store the size of that array.
int main()
{
const int no_of_arrays=10;
int array_size[no_of_arrays]= {1,4,2,4,3,6,8,9,1,3}; // Different size for each array
int *p[no_of_arrays]; // An array of pointers
for(int i=0; i<no_of_arrays; i++)
{
p[i]=new int[array_size[i]+1]; // Allocating
p[i][0]=array_size[i]; // The first cell holds the size of the array
for(int j=1; j<=p[i][0]; j++)
{
p[i][j]=10; // Fill the array with the desired values;
}
}
// Display the arrays
for(int i=0; i<no_of_arrays; i++)
{
for(int j=1; j<=p[i][0]; j++)
{
std::cout<<p[i][j]<<" ";
}
std::cout<<"\n";
}
/*
*
* Do your thing with the arrays.
*
*/
// Deallocate the memory allocated to the arrays
for(int i=0; i<no_of_arrays; i++)
{
delete[] p[i];
}
}
However, doing this is not recommended as it can cause lot of problems ( For example memory leaks in case you forget to use delete after new ). Prefer to use vectors instead in case you don't know the size of the array beforehand.
I have written a program which make a 2d array and then set its numbers.
The second step that I have problem in it is that when I want to shift rows and columns I face with a problem in this line nmatrix[i*c+j] = 0;
the error is this : error: incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)(c + shiftc)) + -1)) + 1)]'
here is the code :
void shiftMatrix(int *matrix, int r,int c ,int shiftr,int shiftc){
int nmatrix [r+shiftr][c+shiftc];
for(int i = 0; i< shiftr; i++)
{
for(int j = 0; j<shiftc;j++)
{
nmatrix[i*c+j] = 0;
}
}
for(int i = shiftr; i< r; i++)
{
for(int j = shiftc; j<c;j++)
{
nmatrix[i*c+j] = matrix[i*c+j];
}
}
}
Any help please??
thanks in advance
int nmatrix [r+shiftr][c+shiftc];
First of all, you are using an array with non-constant bounds, which is a controversial feature.
In addition, here you are declaring a two-dimensional array nmatrix, but your other matrix (matrix) is a pointer to int (or a one-dimensional array, if you like to look at it this way). This is a recipe for confusion.
You can easily declare nmatrix ("new matrix"?) as a one-dimensional array:
int nmatrix[(r+shiftr) * (c+shiftc)];
Or (presumably better)
std::vector<int> nmatrix((r+shiftr) * (c+shiftc));
Then, your code nmatrix[i*c+j] = 0 will work (however, you have to change c to c+shiftc whenever you work with nmatrix).
You cannot define an array dynamically the way you do it.
You need to use the c++ keyword new:
int nmatrix[][] = new int [r+shiftr][c+shiftc];
You cannot define arrays the way you did, with non constant int value for dimension, because such static arrays are to be defined for memory at the compile stage. Thus dimensions should be const expression.
On the contrary with keyword new you can define dimensions for arrays at run-time stage, because it's dynamic allocation.
There are more detailed answers in this SO question here.
This question already has answers here:
How to create 2d array c++?
(5 answers)
Closed 9 years ago.
I have a problem with my code. I have some input for the class, the nmax und mmax. These are defined in header as
int nmax;
int mmax;
Then I have some arrays, defined in header as
double* Nline;
double** NMline;
and then I would like to allocate them in the main program. First, I assign to nmax und max a value from the input
nmax = nmax_in;
mmax = mmax_in;
and then I allocate the arrays
Nline = new double [nmax];
NMline = new double [nmax][mmax];
The problem is, the 1D array is this way allocated. But the 2D array not - the compiler writes:
expression must have a constant value
Why the NLine was allocated and NMline not?
I understand but I don't know how to do it in my program and why for the 1D array this allocation is OK.
Many thanks for your help
double** NMline;
will declare pointer to array of pointers, it will not declare 2D array. You need to first allocate data for the array of pointers (pointers to rows):
NMline = new double*[nmax];
and then to initialize each row:
for(int i = 0; i < nmax; i++)
NMline[i] = new double[mmax];
Don't forget to first delete all rows, and then the NMline itself:
for(int i = 0; i < nmax; i++)
delete [] NMline[i];
delete [] NMline;