Need emergency help with C++ arrays ...

Goosemaster

Lifer
Apr 10, 2001
48,775
3
81
So far this is all I've got:(

I written it MULTIPLE ways and everything points to something like this
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Into the "rows" and "columns" integers. Make sure you pick row-major or column-major ordering to fit what you're going to do with the data, since the performance difference can be significant (if your array doesn't fit in the cache).
 

Goosemaster

Lifer
Apr 10, 2001
48,775
3
81
Originally posted by: CTho9305
Into the "rows" and "columns" integers. Make sure you pick row-major or column-major ordering to fit what you're going to do with the data, since the performance difference can be significant (if your array doesn't fit in the cache).

You just confused me:(


All I want to do is add two two-dimensional arrarys, which is easy, but the kickers si that both must be defined by the user:(
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Hmmm. Well the only thing I can see from the code I posted is the (count+1) should be changed. Actually that whole expression could be changed to something like this:

cout << "\nrow: " << (i + 1) << ", column: " << (j + 1);
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,002
126
You can use an STL vector indexed by another vector to do multi-dimensional arrays.

Best of all its size doesn't need to be initialized as it'll dynamically grow as the user enters data.
 

Goosemaster

Lifer
Apr 10, 2001
48,775
3
81
Originally posted by: BFG10K
You can use an STL vector indexed by another vector to do multi-dimensional arrays.

Best of all its size doesn't need to be initialized as it'll dynamically grow as the user enters data.

I haven't learned those yet:(


I guess I'll have to
 

pkananen

Senior member
Mar 13, 2003
644
0
0
this is tricky to do right. it is easy to do a single dimensional array that maps to a 2 d array, but for a true 2d dynamically allocated array, you have to use pointers. If you haven't learned those yet...it could be tough. I believe you have to have a pointer to a pointer...its been a few years since I did it.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
vector< vector<int> > myArray;
myArray.resize(numRows);
for (int i=0; i<numRows; i++)
myArray.resize(numCols);
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
If STL isn't taught in your class you could get into trouble using it.

Where did you get lost?

clamum's code is the normal way to create a 2-D array in C++ without templates or custom classes.

- pick a size for rows and colums (user could enter this)

(a) create an array of row pointers, loop each row to create a 1-dimensional column array
-or- (b) create an array of column pointers, loop each column to create a 1-dimensional row array

access as a[ row ][ col ] -or- b[ col ][ row ]

To create two of them, use 2 sets of variables and 2 loops.

To fill them, use more loops.

To add them, yet another loop { loop }
 

Goosemaster

Lifer
Apr 10, 2001
48,775
3
81
I GOT IT!
To display it correctly anyways!!!!
THANK YOU ALL SO MUCH:D

Unfortuantely I am practically useless in the software threads, but I will work that much harder in networking as a recompense:)

 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: clamum
Originally posted by: Goosemaster
cool...I believe thI tried that once but where exactly do I put the user input?

Try this

This is right, but note that it is column major order, which is non-standard (unless you're mixing in some fortran). Doesn't hurt, and as CTho said, the right order can make a big difference in some algorithms. But conventional notation is to have the row index first.

And finally, remember to destruct it in the same way you built it - loop over the columns deleting the row arrays, then delete the columns array.