Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Two Dimensional Arrays

Reply
Thread Tools

Two Dimensional Arrays

 
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      04-11-2008
There is a two-dimensional array:

int grades[MAX][MAX];
for( i = 0; i < MAX; i++)
for( j = 0; j < MAX; j++)
grades[i][j] = -1;


I would like to assign an another variable to this array, for example:

int another_grades[MAX][MAX];
for( i = 0; i < MAX; i++)
for( j = 0; j < MAX; j++)
another_grades[i][j] = grades[i][j];

Here, I need to declare another array. Is it possible to have a
pointer to point grades[MAX][MAX]? As I've tried:

int** another_g = grades;

it does not work. Any good idea?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-11-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> There is a two-dimensional array:
>

Please don't multi-post on Usenet, answered elsewhere.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      04-11-2008
(E-Mail Removed) writes:

>Here, I need to declare another array. Is it possible to have a
>pointer to point grades[MAX][MAX]? As I've tried:


>int** another_g = grades;


>it does not work. Any good idea?


The trouble is that unless the compiler knows the shape of the matrix that
another_g points to, it won't be able to work out where (e.g.) element
another_g[3][3] is in memory.

Have a look at
http://burks.bton.ac.uk/burks/langua...trtut/ch7x.htm
 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      04-11-2008
On 11 Apr., 07:37, (E-Mail Removed) wrote:
> There is a two-dimensional array:
>
> int grades[MAX][MAX];
> for( i = 0; i < MAX; i++)
> for( j = 0; j < MAX; j++)
> grades[i][j] = -1;
>
> I would like to assign an another variable to this array, for example:
>
> int another_grades[MAX][MAX];
> for( i = 0; i < MAX; i++)
> for( j = 0; j < MAX; j++)
> another_grades[i][j] = grades[i][j];
>
> Here, I need to declare another array. Is it possible to have a
> pointer to point grades[MAX][MAX]? As I've tried:
>
> int** another_g = grades;
>
> it does not work. Any good idea?


While native arrays are perfectly legal C++, I recommend to use one of
the standard classes std::vector<> or std::valarray<> instead. There
is no performance penalty in using them and only a very small increase
in memory footprint. The benefits by far outweigh the tiny overhead.

I would change your code like this:

#include <vector>
....
std::vector< std::vector<int> > grades( MAX, std::vector<int>( MAX,
-1 ) );
// initializes all elements to -1, no explicit loops!

std::vector< std::vector<int> > another_grades1 = grades;
// copy the whole 2D array to another location.

std::vector< std::vector<int> >& another_grades2 = grades;
// create a reference (implicit pointer) variable to the same
location.

std::vector< std::vector<int> >* another_grades3 = &grades;
// create an explicit pointer to the 2D array.

Access to the std::vector elements looks the same as with native
arrays:

grades[5][3] = 1;
another_grades1[3][5] = 2;
another_grades2[1][1] = 3;
(*another_grades3)[0][0] = 4;

std::valarray<> is an alternative with access methods optimized for
linear algebra at the expense of a less intuitive interface.

best,

Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments