| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Array of two dimension
I am new to the C++ programming. I would like to create a two dimensional array to store values of count X 240. where count is number of particles in a system - shall know only when it is counted.
Pl. help |
|
#2
|
|||
|
|||
|
Re: Array of two dimension
|
|
#3
|
||||
|
||||
|
Re: Array of two dimension
Quote:
![]() just a quick fix would be Code:
#include <iostream>
template <typename T>
T **AllocateDynamicArray( int nRows, int nCols)
{
T **dynamicArray;
dynamicArray = new T*[nRows];
for( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new T [nCols];
return dynamicArray;
}
template <typename T>
void FreeDynamicArray(T** dArray, int nRows)
{
for( int i = 0 ; i < nRows ; i++ ) delete [] dArray[i];
delete [] dArray;
}
using namespace std;
int main()
{
int **my2dArr = AllocateDynamicArray<int>(4,4);
my2dArr[0][0]=5;
my2dArr[2][2]=8;
cout << my2dArr[0][0] << my2dArr[0][1] << endl;
cout << my2dArr[1][1] << my2dArr[2][2]<< endl;
FreeDynamicArray<int>(my2dArr, 4);
return 0;
}
Perhaps someone should email the author with a fix ... I am not
__________________
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010 0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000 This is my c++ object oriented graphical user interface library written for the win32 environment. The main goal is to make it simple to create and maintain a graphical user interface while not sacrificing speed or memory. https://sourceforge.net/projects/cgui-library/ Work in progress, but functional. Help is welcomed. |
|
#4
|
|||
|
|||
|
Re: Array of two dimension
Quote:
You really only need to call the allocator twice, regardless of the size of the array. Here is a better version: Code:
#include <iostream>
template <typename T>
T **AllocateDynamicArray( int nRows, int nCols)
{
T **dynamicArray;
dynamicArray = new T*[nRows];
T* pool = new T[nRows*nCols];
for( int i = 0 ; i < nRows ; i++, pool += nCols )
dynamicArray[i] = pool;
return dynamicArray;
}
template <typename T>
void FreeDynamicArray(T** dArray)
{
delete [] dArray[0];
delete [] dArray;
}
Regards, Paul McKenzie Last edited by Paul McKenzie; January 1st, 2010 at 06:05 AM. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|