-
2d Dynamic Array help
Okay, so I'm writing this program to add 2 dynamic arrays(they're matricies) and put the contents in a 3rd dynamic array that has it rows size based on the smaller of the two addend arrays, ditto for the columns. I seriously thought that it would be a simple matter of simply assigning the temporary variable two subscripts but obviously I was wrong and my book while easy to read, doesn't do a real good job with dynamic arrays(or much else, it's too wordy) and doesn't cover 2d Dynamics at all. :angry:
My monstrosity looks like this:
Code:
#include <iostream>
using namespace std;
typedef float * FloatP;
void main()
{
int a1r=1,a1c=1,a2r=1,a2c=1,a3r=1,a3c=1, row, col;
FloatP A1,A2,A3;
A1 = new float[a1r][a1c];
A2 = new float[a2r][a2c];
A3 = new float[a3r][a3c];
cout<<"enter a1's row and columns"<<endl;
cin>> a1r >> a2r;
cout << "enter a2's rows and columns" << endl;
cin >>a2r>>a2c;
if(a1r<a2r)
{
a3r=a1r;
}
else
{
a3r=a2r;
}
if(a1c<a2c)
{
a3c=a1c;
}
else
{
a3c=a2c;
}
for(row=0; row<a3r; row++)
{
for(col=0; col<a3c; col++)
{
A3[row][col]=A1[row][col]+A2[row][col];
}
}
for(row=0; row<a3r; row++)
{
for(col=0; col<a3c; col++)
{
cout<<A3[row][col];
}
}
}
Obviously it's wrong, but I don't understand why. My book said one the pointers were attached to a temporary variable that they could be treated like a regular array.
HELP!
"The avalanche has already started, it is too late for the pebbles to vote" Kosh
-
you cannot declare this the way you did it.
do it this way:
float ** array;
array = new float *[some_size];
for(x = 0....some_size .. etc)
array[x] = new float[other size];
remember to delete in loop before you delete the outside pointer.
This is kinda painful... vectors or valarrays can help (I think there is a slice accessor thing that can really make it clean!), or
use a one D pointer and manage index yourself (many matrix packages do it that way for many reasons).
-
Well, this is a bit old, but... How about a C function that creates 2-d arrays. I've been using it since before C++ was around and never had a reason to update it. (Hey, it it works, don't mess with it <g>). Note: BYTE = unsigned char
// Create a dynamic 2-d array
BYTE** AllocArray(int iRows, int iCols, int iBytesPerVal)
{
// Allocate memory for both the row pointers and the data
BYTE **ppData;
int iBytes = iRows * sizeof(BYTE*) + iRows * iCols * iBytesPerVal;
if ((ppData = (BYTE**)malloc(iBytes)) == NULL)
return NULL;
memset(ppData, 0, iBytes);
// Set the row pointers to the start of each row
ppData[0] = (BYTE*)(ppData + iRows);
for (int i = 1; i < iRows; i++)
ppData[i] = ppData[i-1] + iCols * iBytesPerVal;
return ppData;
}
Example:To allocate a 4x5 floating point array -
float **ppfData = (float**)AllocArray(4, 5, sizeof(float));
You can now access your array just like you expect -
ppfData[1][0] = 23.4f;
float fVal = ppfData[2][3];
Remember when you're done to use free(ppfData) rather than delete. Hope this helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks