Friday, July 31, 2009

C++ programming help, point me in the right direction?

Anyone know how to create a 3D array of doubles that has dimensions of [1000] [100] [10] and setting all the elements of the array to 1.0 using dynamic memory allocation? Then how do you check to make sure it was allocated successfully?





Then finally how would you sum all the elements in the array above, output the result, and then free the allocated memory?


this is what I have :





#include %26lt;iostream%26gt;





using namespace std;





int main()


{





double p3darr [1000][100][10];





return 0;


}





It compiles but gives an "unreferenced local variable" warning, what does that mean?

C++ programming help, point me in the right direction?
(1) You need to use operator new[] to allocate your array dynamically.





(2) Checking for memory allocation is easy enough if you have a modern compiler. By default new should throw the standard exception bad_alloc if something goes wrong. If you have an older (or non-compliant) compiler it is a little more complicated and worry about that if it is actually the case.





(3) Given that (2) throws and exception you need to use try/catch blocks.





(4) You free the memory with delete[]. Remember that new goes with delete and new[] goes with delete[] for arrays.





(5) Now the somewhat hairy stuff. The syntax for arrays beyond 2 dimensions gets pretty funky. It is easier to go about it multiple steps. I find using typedefs simplifies things somewhat and helps me keep things straight. In this case I typedef-ed a 2 dimensional array of doubles as type d2type. I then created an array of that d2type giving a 3d array. You can figure it out from there.





#include %26lt;iostream%26gt;


#include %26lt;new%26gt;





using namespace std;





int main()


{


typedef double d2type[100][10];





const int x = 1000, y = 100, z = 10;





try


{


d2type *p3darr = new d2type[1000];





//init array


for (int i = 0; i %26lt; x; ++i)


{


for (int j = 0; j %26lt; y; ++j)


{


for (int k = 0; k %26lt; z; ++k)


{


p3darr[i][j][k] = 0.0;


}


}


}





//


//presumably do something to array here


//





//print array


for (int i = 0; i %26lt; x; ++i)


{


for (int j = 0; j %26lt; y; ++j)


{


for (int k = 0; k %26lt; z; ++k)


{


cout %26lt;%26lt; p3darr[i][j][k] %26lt;%26lt; endl;


}


}


}





delete[] p3darr;


}





catch (bad_alloc %26amp;e)


{


cerr %26lt;%26lt; "Error: " %26lt;%26lt; e.what() %26lt;%26lt; endl;


exit(1);


}





return 0;


}
Reply:It simply means p3darr has been declared but never used.


To use dynamic memory allocation you must allocate(sizeof(p3darr)) and then use pointers to store your data.


Go back through to check that each location has a 1.0, printf the result or %26gt;%26gt; then release the memory but I'm not doing it for you!
Reply:i started to try and learn that. Im young but i tried. didnt do it for long. It is pretty difficult stuff

800flowers.com

No comments:

Post a Comment