I have this function that reads a textfile and sends the strings to an array and then from that function I send that array and number of elements to my constructor. Now my constructor creates a dynamic 2d-array(I hope). I would like my 2d-arrays rows and columns to be assigned values from the array received.
heres my constructor.
Graph::Graph(string cities[], int n)
{
this->nrOfCities=n;
this->x=n;
this->y=n;
this->graph=new string *[x];
for (int i = 0; i < x; i++)
this->graph[i] =new string[y];
for(int i=0;i<this->x;i++)
for(int j=0;j<this->x;j++)
this->graph[j]=NULL;
for(int i=0;i<=this->x;i++)//I know this last part doesn't work.
for(int j=0;j<this->x;j++)
this->graph[0][j+1]=cities[j];
}
Any kind of help is appreciated.
In order to make a dynamic 2darray you must try s.th. like this:
type** arr2d;
arr2d = new type*[rows];
for(int i=0; i<rows; ++i)
arr2d[i] = new type[cols];
Related
Hy guys, I actually Trying to create a 2D Array in c++ but not able to create that, When I execute the following statement
int arr=new int[10][10]
It gives me error and when I search on google it shows me 2D array in c++ is array of pointers which is declare like the below statements
int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
a[i] = new int[colCount];
I got the logic which is a is a pointer to pointer to the matrix but now I am not able understand the logic like how can i point to the data on this matrix, Suppose to see the number store in index a[0][0] should i write
cout<<a[0][0]
or not, I am not able to get the logic how this pointer to pointer will work when with the pointers pointing to the matrix, and one more thing is that I am not able to pass it as an argument to a function. The code for passing it as a parameter is given below
void displayArray(int a[10][10])
{
for (int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<*a[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int** a = new int*[10];
for(int i = 0; i < 10; ++i)
a[i] = new int[10];
displayArray(**a);
}
It giving me the following error
error: invalid conversion from ‘int’ to ‘int (*)[10]’ [-fpermissive]
Actually I am not able to get any sense of how to use the pointer to pointer in a matrix, it's too complex compared to other languages where we just need to use new operator and can access them with their dimensions, No need of this pointer to pointer concept. Please help me understanding the whole logic of this 2d dynamic array of c++.
you need to get the parameter in your function as pointer
void displayArray(int **a)
{
for (int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<< a[i][j] <<"\t";
}
cout<<endl;
}
}
int main()
{
int** a = new int*[10];
for(int i = 0; i < 10; ++i)
a[i] = new int[10];
displayArray(a);
}
it prints 10 rows and columns of value 0 because the 2D array is uninitialized
I am relatively new to programming and am trying to read an array of ints into a 2-D dynamic array. I'm relatively sure this is just a syntax issue.
The array is dynamic and therefore is of type int**. To try to populate the array, I've used a nested for loop to populate each element of the array with the next term from the ifstream. Input is assumed to be all ints separated by
whitespace.
//declare dynamic 2-D array
int** myArray = new int*[numRows]
for (int i = 0; i < numCols; i++)
{
myArray[i] = new int[numCols];
}
//populate array from ifstream
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
inFile >> myArray[i][j];
}
}
I expected to be able to store the ints from the ifstream (all between 0-100) directly into the array, but I seem to be storing addresses instead (very
large negative ints). What am I doing wrong?
Fixing the typo and a missing semicolon should fix it
// declare dynamic 2-D array
int** myArray = new int*[numRows]
// ^
// ;
for (int i = 0; i < numCols; i++) {
// ^^^^^^^
// numRows
myArray[i] = new int[numCols];
}
I have a 2D array with size 4 (4 rows). To make the array size 2 (2 rows), can I use following? (for our hw assignment details are not specified and the code should be suitable with common c++ standarts)
I am removing second half of the array.
const int newSize = flightsArraySize/2;
for(int i = newSize-1; i < flightsArraySize-1; i++)
delete [] flights[i];
Or do I have to recreate flights array with size 2?
Supposing that you have created a 2D array using new like this:
int **arr = new int*[rows];
for(int i=0; i<rows; ++i)
arr[i] = new int[cols];
Then to resize it you'd have to do something like:
int newRows = rows/2;
// Create a new array for the right number of rows.
int **newArr = new int*[newRows];
// Copy the rows we're keeping across.
for(int i=0; i<newRows; ++i)
newArr[i] = arr[i];
// Delete the rows we no longer need.
for(int i=newRows; i<rows; ++i)
delete[] arr[i];
// Delete the old array.
delete[] arr;
// Replace the old array and row count with the new ones.
arr = newArr;
rows = newRows;
But seriously, this is all so much easier if you just use vector:
std::vector<std::vector<int>> v(rows);
for(int i=0; i<rows; ++i)
v[i].resize(cols);
v.resize(v.size()/2);
Well, it deallocates the memory on which pointed the second half of pointers. But the poiters themselves will stay, the array of pointers will not be shortened.
EDIT
Oh, sorry. It seems as a mistake. If you have code like this:
int **ptr = new int*[4];
for(int i = 0; i < 4; i++)
{
ptr[i] = new int[4];
}
Then when you type
delete[] ptr[3];
It will delete the whole array, so you can create new like this:
ptr[3] = new int[any_number];
Is this what you mean? Sorry, I read too fast...
How can I dynamically allocate a two dimensional array where each row contains variable element? How can I free this memory?
Actually you probably want a vector of vectors, something like this:
std::vector<std::vector<int> > vector_2d;
This way you'll benefit from the automatic memory management of the vector rather than having to manually manage the memory of an array of pointers to arrays of data.
You would need need to use pointers:
int ** a;
a = new (int*)[5];
for(int i = 0; i<5; i++)
{
a[i] = new int[x]; //where x is the size of this row
}
Something like this should work, but I didn't test it.
Just for the sake of completeness:
int ** a;
a = new (int*)[5];
for(int i = 0; i<5; i++)
{
a[i] = new int[x]; //where x is the size of this row
}
for(int i = 0; i<5; i++)
{
delete[] a[i];
}
detete[] a;
I am making a C++ program that checks if given aray is a latin square. I need to use a dynamic multi-dimensional array that stores given latin square. But I cant pass the array to a function that does the checking...
Currently I have such code for calling the function:
int squaretest(int **p, int n, int sum) {
//some code
};
And this code is for creating the array:
int main() {
//some code. n - length of one row, sum - sum of elements in one row.
int a;
int **lsquare;
lsquare = new int*[n];
for (int i=0;i<=n-1;i++) for (int j=0;j<=n-1;j++) {
cin >>a;
lsquare[i][j] = a;
}
blocktest(lsquare,n,sum);
//some code
};
The code compiles (i am using Geany IDE and G++ compiler) but when I run it in terminal, after the first imput, that has to be stored in block[0][0] I get Segmentation fault error. What's wrong with my code and what is the correct sollution?
To be able to do that.. You actually need to do this:
int **lsquare = new int*[n];
for (int i=0; i<n; ++i)
lquare[i] = new int[n];
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
cin >> lsquare[i][j];
blocktest(lsquare,n,sum);
The better system would be to do:
int *lsquare = new int[n*n];
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
cin >> lsquare[i + j*n];
blocktest(lsquare, n, sum);
You forgot to allocate memory for second dimension of the matrix.
int **lsquare;
lsquare = new int*[n];
for (int i=0; i<n; ++i){
lsquare[i] = new int[n];
....}
nobody writes
for (int i=0;i<=n-1;i++){...}
Do instead
for (int i=0; i<n; ++i){...}
You have an array of pointers in lsquare.
You might want to just do something like:
lsquare = new int[n * n];
That way you can then fill in this square, but the type is then:
int *lsquare
What you are actually creating an array of arrays. Not only do you need to allocate the array of arrays using new, but also you must allocate all n arrays. You'll want to have the outer loop of your nested for loop allocate each of the n sub-arrays.
lsquare = new int*[n];
for (int i=0;i<=n-1;i++)
{
lsquare[i] = new int[n];
for (int j = 0;j<=n-1;j++)
{
//...
You made yourself a pointer pointer that can be used as a matrix, allocated one row for it, then proceeded to act like you'd allocated an entire n*n matrix. You will indeed get a segfault if you run that.
You need to allocate enough space for n*n elements, not just n of them.
A less error-prone solution might be to use a std::vector of std::vectors.
You have to allocate space for the second dimension too, add this after you allocate lsquare:
for(int i = 0; i < n; ++i)
{
lsquare[i] = new int[n];
}