
- Forum
- Programming Talk
- C and C++
- Multi-Dimensional Arrays
Multi-Dimensional Arrays
This is a discussion on Multi-Dimensional Arrays within the C and C++ forums, part of the Programming Talk category; Hi guys, I was wondering if some1 out there could help me a little with my c++ work i gotta ...
-
Multi-Dimensional Arrays
Hi guys,
I was wondering if some1 out there could help me a little with my c++ work i gotta do!? Im a bit of a c++ rookie and am having issues setting up a multi-dimensional array in which the user can input the rows and columns. Its a relatively simple program (to which loads more has to be added!) but I cant even get this bit to work. Any help would be greatly appreciated!
int main()
{
int dx,dy;
cout << "Enter the width of the matrix\n";
cin >> dx;
cout << "Enter the height of the matrix\n";
cin >> dy;
int MyArray[dx][dy][5] = {0};
for (int i=0; i<dx; i++)
for (int j=0; j<dy; j++)
for (int k=0; k<5; k++)
{
cout << "MyArray[" << i << "][ " << j << "]: ";
cout << MyArray[i][j][k] << endl;
}
return 0;
}
-
Hi,
Arrays are used to reserve a memory block but the drawback in this is knowing the size of the array before we write a program also that array size must be a constant as reuired by the compiler.So what you have written will not work because of this reason.
But C++ provides memory management operators namely: new and delete . They are used to allacate and free blocks of memeory respectively.
That is object is created by new and destroyed by delete as and when required.
So you can use this concept to solve your program
Syntax of new is :
pointer variable = new datatype
Sripriya
-
Priyaraji, please dont place unhelpful posts.
falco85, heres a code sample - you may need to change a few things, but I'm sure you'll understand the pointers utilization.
Code:#include "iostream.h" #include "malloc.h" int main() { //I'm leaving each array index with 4 bytes - that means the INT method we're using //will not overlap memory regions int dx=5,dy=5; cout << "Enter the width of the matrix\n"; cin >> dx; cout << "Enter the height of the matrix\n"; cin >> dy; int *MyArray=0; MyArray=(int*)malloc(dx*dy*5*8); int *myPoint=0; cout<<"Filling array\n"; int i=0,k=0,j=0; for (k=0; k<5; k++) { for (j=0; j<dy; j++) { for (i=0; i<dx; i++) { MyArray[(i+(j*dx)+(k*dy*dx))*4]=(i+(j*dx)+(k*dy*dx)); } } } cout<<"Printing array\n"; for (k=0; k<5; k++) { for (j=0; j<dy; j++) { for (i=0; i<dx; i++) { cout << "MyArray[" << i << "][ " << j << "]: "; cout << MyArray[(i+(j*dx)+(k*dy*dx))*4] << endl; } } } cin>>dx; free(MyArray); return 0; }
Have FUN!
Chaitanya
-
By the way, if you are creating a large program using this to store data or something, I suggest you do not. It is better to use some form of clustering using Classes, in C++ it will be easier to manage.
For an array, you will have to physically calculate the address - multi-dimensionsal arrays will not work exactly right if you dont worry about numbers.
What you could do is make a class that contains the independent arrays - and then initialize them using a wrapper class, that makes an array of each class.
eg. Class A, B and C contain one array each. Class D contains 3 seperate arrays - one of A, one of B and one of C. Hence you have a two layer set of arrays, basically the same as D[x][y][z] if you understand it better that way.
Do another layer and you get a D[][][][] set, and so on. This is accessed by a function written in each class, thereby making it easier to use.
If you'd like more help on this matter dont hesitate to ask.
Chaitanya
-
Sponsored Ads

Reply With Quote





