# dynamic multidimensional arrays Example

### By ImArrow on May 01, 2012

A dynamic array is an array data structure that can be resized and which allows elements to be added or removed.

7-11-12 UPDATE Optimization HELP by FirstMate :)

``````//creation of dynamic multidimensional arrays?
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//For creating one dimension array of 4 integers
int *ptr1;
ptr1 = new int;
for( int i = 0 ; i < 4 ; i++)
{
ptr1[i] = i+1;
}

cout << "\nOne dimension array of 4 integers:\n";
for( int i = 0 ; i < 4 ; i++)
{
cout << "  " << ptr1[i] ;
}

cout << endl << endl;

//for creating two dimensional array of 3 by 4 integers
int **ptr2;
ptr2 = new int*;
for( int i = 0 ; i < 3 ; i++)
{
ptr2[i] = new int;
}

for( int i = 0 ; i < 3 ; i++)
{
for ( int j = 0 ; j < 4 ; j++)
{
ptr2[i][j] = i * 4 + j;
}
}

cout << "\nTwo dimensions array of 3 by 4 integers:\n";
for( int i = 0 ; i < 3 ; i++)
{
for ( int j = 0 ; j < 4 ; j++)
{
cout << setw(4) << right << ptr2[i][j] << " ";
}
cout << endl;
}

//for creating three dimensional array of 2 by 3 by 4 double float
double ***ptr3;
ptr3 = new double**;
for( int i = 0 ; i < 2 ; i++)
{
ptr3[i] = new  double*;
for( int j = 0 ; j < 3 ; j++)
{
ptr3[i][j] = new  double;
for( int k = 0 ; k < 4 ; k++)
{
ptr3[i][j][k] = ( i * 3 + j ) * 4.1 + k * 0.1;
}
}
}
cout << "\nThree dimensions array of 2 by 3 by 4 double float :\n";
for( int i = 0 ; i < 2 ; i++)
{
for( int j = 0 ; j < 3 ; j++)
{
for( int k = 0 ; k < 4 ; k++)
{
cout <<"\nptr3(" << i << "," << j << "," << k << ") = " << setprecision(4) <<
{
ptr3[i][j][k];
}
}
}
}

return 0;
}

``````

ImArrow   -  Jul 11, 2012

Yes i can thanks. code has been Updated :)

Firstmate   -  May 03, 2012

Consider it? If it's a matter of performance (and I'm not expert on the matter), in your snippet you're iterating so much more than necessary. Consider your 2x3x4 example. Mine would iterate (234) or 24 times. In your case, you loop 2 + (23) + (23*4) or 32 times. And these are just small dimensions. Again, I'm not too knowledgeable but surely you can see the optimization in this.

ImArrow   -  May 03, 2012

Hmm interesting outtake firstmake. ill consider it

Firstmate   -  May 02, 2012

I'm not sure if this was done more out of demonstrating the code, but why not take:

``````    double ***ptr3;
ptr3 = new double**;
for( int i = 0 ; i < 2 ; i++)
{
ptr3[i] = new  double*;
}
for( int i = 0 ; i < 2 ; i++)
{
for( int j = 0 ; j < 3 ; j++)
{
ptr3[i][j] = new  double;
}
}

for( int i = 0 ; i < 2 ; i++)
{
for( int j = 0 ; j < 3 ; j++)
{
for( int k = 0 ; k < 4 ; k++)
{
ptr3[i][j][k] = ( i * 3 + j ) * 4.1 + k * 0.1;
}
}
}
``````

And make it:

``````    double ***ptr3;
ptr3 = new double**;
for( int i = 0 ; i < 2 ; i++)
{
ptr3[i] = new  double*;
for( int j = 0 ; j < 3 ; j++)
{
ptr3[i][j] = new  double;
for( int k = 0 ; k < 4 ; k++)
{
ptr3[i][j][k] = ( i * 3 + j ) * 4.1 + k * 0.1;
}
}
}
``````
Sorasyn   -  May 01, 2012

I'm more than confident I'll be taking a C++ class in the near future. I'll definitely hit you up if I need any help in doing so.

ImArrow   -  May 01, 2012

yes patience is definitely a key factor in any language. AND IF YOU EVER DO TAKE C++ AND HAVE ANY TROUBLE. ask me :P

Sorasyn   -  May 01, 2012

Yeah, I've tried to take a somewhat interest in C++ since I'm sure my college courses will include it at some point in time. However, like any language, it takes an enormous amount of patience to do. Although, maybe I'll get around to learning it some day.

ImArrow   -  May 01, 2012

Well thanks :P c++ is fun,i heard java is fun but its hard to learn, but eh. i once found a java code online that made me giggle....at the noobness of the person behind it. maybe it will make you laugh as well :P

protected void setValid(boolean valid) {
this.valid = true;
}

Sorasyn   -  May 01, 2012

I could say the same for C++. ;) Looks good.

ImArrow   -  May 01, 2012

Indeed, yes they do, now in that sense java is foreign to me, java actually intimidates me. :P

Sorasyn   -  May 01, 2012

Interesting concept. I was just researching into Java's ability to dynamically adapt an arrays index count to allow for infinite entries not 10 minutes ago. Although, most arrays, by default, allow the definition, or replacement of already present indexes.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.