Michael Reaves wrote:
> Group,
>
> I am trying to create a dynamic 2 dimensional array. I have to read in 2
> numbers from a file. These 2 numbers tell me what size array I need. How
> do I take these 2 numbers and create a dynamic 2 dimensional array? For
> example I read in 3 3 and need an array of points[3][3]. In the same file
> later I read in 4 4 and need the same array points[4][4]. All suggestions
> will be appreciated.
>
> Thanks,
>
> Michael J. Reaves, MCP
> Database Administrator/Financial Coordinator
Don't try, just do. With Safe C++, I would do something like:
size_t x, y, Width, Height;
Block< Block<int> > Points;
while (cin >> Width >> Height)
{
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width; x++)
{
cin >> Points[y][x];
}
}
}
Should work (I don't use 'cin' so no guarantees). Not the most
efficient code ever, but it gets the job done pretty quickly. Being a
DBA, you probably care most about getting stuff working and then MAYBE
going back and tweaking it for performance. Safe C++ Block
automatically resizes itself when referencing out-of-bounds memory.
(Safe C++ Design Principles is a free e-book for c-prog members in the
Files section). You may be able to get the equivalent effect with
vector<> but you will have to call size() before accessing data.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* VerifyMyPC 2.3
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.
Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/