Community discussion forum
2D Dynamic Arrays in C++
-
Hi, I've been searching for a way to declare and use 2Dimensional and Dynamic Arrays.
I tryed using malloc:
I built a 1D Dynamic Array;
Created a Struct with this 1D Array inside
then i created another 1D Dynamic Array containing these Structs.
Then I tryed to access the data like this: arrayOfStructs.arrayInsideStruct[j], but it didnot work.
Could anyone help me? -
Advertisement
Simply the fastest line-level profiler for .NET ever
“The low overhead means it has minimal impact on the execution of my program”
Mark Everest, Development Team Leader, Renault F1 Team Ltd.Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now
-
slap some source code here. that always helps.
ohh, and dont bother with malloc. that was replaced by the 'new' operator in c++.
by dynamic array, do you mean an array of variable size ? if so, you will need to use a vector class.
anyways... try somthing like this...
Code:
include <iostream>
using namespace std;
int main() {
const int MAXX=5, MAXY=10;
int x,y;
int my2DArray[MAXX][MAXY];
for (y=0; y<MAXY; y++) {
for (x=0;x<MAXX; x++) {
my2DArray[x][y] = x + y;
}
}
for (y=0; y<MAXY; y++) {
for (x=0;x<MAXX; x++) {
cout << my2DArray[x][y] << ' ';
}
cout << endl;
}
return 0;
}
an example i wrote on how 2d arrays work. -
so here is ur solution of dynamic memory allocation
reply me soon ..........................include<iostream.h>
void main()
{
int **a,m,n; // double pointer for 2d arry
cout<<" \n enter the number of rows and coloums for matrix:";
cin>>m>>n;
a=new int *[m]; // dynamic allocation of pointers array for rows
for(int i=0;i<m;i++)
a=new int [n]; // dynamic allocation of coloums for each rows
cout<<"\n enter the elements:";
for(i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[j];
cout<<"\n the elements r:";
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<"\n";
}
} -
Hey, i'm new to C++ and was doing a school project on 2D arrays. the 2nd code posted here doesn't work
a=new int [n]; // dynamic allocation of coloums for each rows
here or somewhere near here the compiler says that '=' cannot convert from 'int' to 'int*'
and there's another place that has problems with type conversion -
Hi'
I think vector<vector<Type>> dynamic2darray may work.
Have a try!
-
Hi!
to dynamically allocate a 2D array, i recently used the following code. This doesn't directly creates a 2-d array. It actually creates a 1-D array of size m*n (where m and n are the num of rows n cols in the reqd 2-d array), then uses an addressing technique to access the i,j element:
float fArray; //declares an int pointer for array
int m,n; //the size of array
cin>>m>>n;
fArray = new float[mn]; //allocates memory for 1-d array of size m*m
now, u can access the [j] element by the following expression:
((float *)fArray + (im) + j)
so, everywhere, u want to refer to fArray[j], just write the above expression.
Enjoy! -
Hi, I am a newbie to this forum and this is my first post. I am learning C++ with a book "Financial Modeling Using C++". I encountered some compile time errors for the source codes included there, in particular multidimensional dynamic array examples.
Since the publisher's website does not indicate any correction information and the author's contact information is not available, I would like to seek an advice here.
Please see the following code:
// Example 11.8: 3-D dynamic array
#include
<iostream>using
namespace std;int main(){
int i, j, k, m, dim1, dim2, dim3;cout << "Enter array dimensions: ";cin >> dim1 >> dim2 >> dim3;
int (*pa)[dim2][dim3] = new int[dim1][dim2][dim3]; // error C2057 // initialize arraym = 0;
for (i = 0; i < dim1; i++) for (j = 0; j < dim2; j++)for (k = 0; k < dim3; k++)pa[i][j][k] = m++;
cout << "Value of element (0,0,0): " << pa[0][0][0] << endl;cout <<
"Value of element (1,2,1): " << pa[1][2][1] << endl;cout << "Value of last element: " << pa[dim1 - 1][dim2 - 1][dim3 - 1];cout <<
"\n\n";delete[] pa; // Wait for the user to read the output on the consolesystem("PAUSE");return 0;}
The first message of compile time errors I had is "error C2057: expected constant expression" regarding the declaration of pointer variable.
Could you kindly provide me with any suggestion for correction? Since I am still a novice, I would appreciate a suggestion without use of advanced concepts in C++. Many thanks in advance.
-
#include <iostream>using namespace std;
int main()
{int i, j, k, m, dim1, dim2, dim3;cout << "Enter array dimensions: ";
cin >> dim1 >> dim2 >> dim3;//int (*pa)[dim2][dim3] = new int[dim1][dim2][dim3]; // error C2057 [old code segment]
/*new rectified lines of code start*/
int ***pa = new int **[dim1];
// initialize array
for (i = 0; i < dim1; i++) {
pa[i]= new int *[dim2];
}for (i = 0; i < dim1; i++) {
for (j = 0; j < dim2; j++) {
pa[i][j]= new int[dim3];
}
}/*new rectified lines of code end*/
m = 0;
for (i = 0; i < dim1; i++)
for (j = 0; j < dim2; j++)for (k = 0; k < dim3; k++)
pa[i][j][k] = m++;
cout << "Value of element (0,0,0): " << pa[0][0][0] << endl;
cout << "Value of element (1,2,1): " << pa[1][2][1] << endl; //[dim1,dim2,dim3 expected values- >= 1,2,1]cout << "Value of last element: " << pa[dim1 - 1][dim2 - 1][dim3 - 1];
cout << "\n\n";delete[] pa;
// Wait for the user to read the output on the consolesystem("PAUSE");return 0;
} -
Thank you very much, sidd.biswas!!! I feel I need to learn how to use multiple dereference operators "*" by refering to some material other than the book I mentioned above, which lacks proper explanations.
-
-
According to your title, you are writing in C++. I suggest you use vectors (std::vector). These are dynamic.
If the data you want to save in the arrays needs to be indexed by name, use a map instead (std::map)For more info, look here: http://www.cplusplus.com/
Click on the "STL: Standard Template Library" link.
Good luck.
Alexis -
try this code for dynamic memory allocation for two dimensional array . this program is dispaly random number
include !--removed tag-->!--removed tag--> include !--removed tag-->!--removed tag-->using namespace std;
void main() { int **r,n;
int i,j; cout <<"Enter a Number"; cin >> n; r = new int *[n]; for(int i=0;i<n;i++) r[i] = new int [n]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { r[i][j] = rand()/1000; cout << r[i][j]<<"\t"; } cout<<"\n"; }}
-
Is anyone interested in working on Graphics Technologies within the Windows Platform? -Ryan Phillips engineerfinder@live.com
-
yes dear why not but what is work
Post a reply
Quick links
Recent activity
- gangireddysaritha replied to i have struck with my proj...
- Stefano Sloop replied to The buzz of economic activity
- jack semwal replied to i have struck with my proj...
- Larry Bargers replied to Error 4 Make sure that the ...
- Ashok Singh replied to How to receive data in web ...
- Cheryl Tan replied to File Upload.. how?
Enter your message below
Sign in or Join us (it's free).