|
|
C++ Dynamically Allocated Multi-dimensional Array
|
|
|
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
I'm working on a program right now for a CS class that's a treasure map. We're supposed to read in a data file and dynamically create the array to store it.
I create my array with the following statement:
int **newGrid = new int[initialRows][initialCols];
where initialRows and initalCols are interger values of the size the array needs to be.
However, I get the following message from g++ on the compile:
g++ -Wall -c treasure.cpp
treasure.cpp: In constructor `TreasureMap::TreasureMap(int, int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
treasure.cpp:19: cannot convert `int (*)[((initialCols - 1) + 1)]' to `int**'
in initialization
make: *** [treasure.o] Error 1
Can anyone tell me what is wrong? Do I have my syntax set up incorrectly? Am I just stupid?
Thanks.
(
Last edited by fromthecloud; Oct 23, 2003 at 12:32 PM.
)
|
chown -R us:us yourbase
Dissent is not un-American.
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
to make a 2dimensional array in c++ you need to make a one dimmensional arraythen loop through and add the second dimension
p = new int*[ n ];
for ( i < n )
p[ i ] = new int[ m ];
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status:
Offline
|
|
I would personally make it:
int *newGrid = new int[initialRows * initialCols];
...it seems to be what you're trying to go for...
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status:
Offline
|
|
Originally posted by fromthecloud:
I'm working on a program right now for a CS class that's a treasure map. We're supposed to read in a data file and dynamically create the array to store it.
I create my array with the following statement:
int **newGrid = new int[initialRows][initialCols];
where initialRows and initalCols are interger values of the size the array needs to be.
However, I get the following message from g++ on the compile:
g++ -Wall -c treasure.cpp
treasure.cpp: In constructor `TreasureMap::TreasureMap(int, int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
treasure.cpp:19: cannot convert `int (*)[((initialCols - 1) + 1)]' to `int**'
in initialization
make: *** [treasure.o] Error 1
Can anyone tell me what is wrong? Do I have my syntax set up incorrectly? Am I just stupid?
Thanks.
Even though the C++ standard says you can do this, I've never gotten it to work. I agree with the other posters.
WHSoft
I have a matrix class that I set up to do exactly what you are wanting to do. Don't copy the code, because you will be expelled from school for it, but if you are stumbling across odd bugs in your code, this class has been rather extensively tested. You can compare your method to mine to see if there's anything obvious.
|
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
yeah the probably faster way would be the one dimensional array and do the math to make it seem 1 dimensional though indexing on the second axis may not be the fastest.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
edit: was wrong, AND somehow managed to double post
(
Last edited by Gul Banana; Oct 25, 2003 at 12:40 PM.
)
|
[vash:~] banana% killall killall
Terminated
|
|
|
|
|
|
|
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
|
[vash:~] banana% killall killall
Terminated
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
Bananna, man... that'll make one BIG array, not a multi-dimensional one. That said, this compiles w/o errors:
Code:
int main (int argc, const char * argv[]) {
int rows = 4;
int columns = 6;
int **x = new int* [rows];
for(int i =0;i< rows;i++)
{
x[i] = new int[columns];
}
return 0;
}
hope that helps.
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
|
|
|
|
|
|
|
Registered User
Join Date: Oct 2003
Status:
Offline
|
|
cheerios wrote:
that'll make one BIG array, not a multi-dimensional one.
Hmm. Maybe a post was edited, but it looks like you may have really been replying to smitty825. If so, I think he meant for the OP to fake a multi-dimensional array like so:
Code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "number of rows?: ";
int rows;
cin >> rows;
cout << "number of columns?: ";
int cols;
cin >> cols;
int * array = new int[ rows * cols ];
// Fill 'em up. For a given row:
for ( int y = 0; y < rows; ++y )
{
// Move your way across all the columns:
for ( int x = 0; x < cols; ++x )
{
array[ ( y * cols ) + x ] = x + y;
}
}
// Print 'em out, right-side-up though.
cout << "\n";
for ( int y = rows - 1; y != -1; --y )
{
// Move your way across all the columns:
for ( int x = 0; x < cols; ++x )
{
cout << array[ ( y * cols ) + x ] << "\t";
}
cout << "\n";
}
cout << endl;
return 0;
}
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by cheerios:
Bananna, man... that'll make one BIG array, not a multi-dimensional one.
What's the difference? The dimensions are just an easy way of referencing different elements within a chunk of memory.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Feb 2003
Location: Portland, Oregon
Status:
Offline
|
|
I did an assignment like this last term, accept ours was a 3D array. After long hours of research and struggling, I found out that the best way to go about this is to overload the [] operator, which is very simple and allows you to create a very elegant nD array.
I would give you more clue-ins on how to do the [] operator, but researching how to do this is what makes the assignment beneficial. I"ll give you a hint though, if your [] operator is > 5 lines of code, you're thinking about it at too low a level of abstraction. Think what what you want to do, instead of how.
jesse ;-)
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
ah, excuse me, I didn't fully understand what he was sayin'. got that 2D array in my head, and ddn't wanna think about nuffin' else
and yes, Bannana had a solution somewhat similar to JohnMG's. And, on thinking, it makes sense, just isn't exactly what the origional guy was askin' about, and would take a TEENY BIT more effort to keep track of where the "rows" were, when you wanted to access it. :shrug: Oh, and w/ school, a lot of times, they say "do it my way" and you had better!
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|