473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic array advice.

I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}

Mar 3 '07 #1
9 2103
JoeC wrote:
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}
That would work. There are other lots of other approaches.

The classic is (and simplest) to use double pointers

space** spaces = new space*[width];
for (int i = 0; i < width; ++i)
spaces[i] = new space[length];

then you can just say

space[i][j] = whatever;

and when you want to free the memory, do the reverse

for (int i = 0; i < width; ++i)
delete[] spaces[i];
delete[] spaces;

john
Mar 3 '07 #2
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}
I am getting rather tired of complaints about not being able to do 2-D arrays.

Here is a multidimensiona l array class derived from std::vector that I wrote and
have used for many years. It is not optimal, but is close enough for all but the
most time intensive programs.

If this not sufficient, the I would suggest using the Boost::multiarr ay.
//=============== ========= Array.h =============== =============== =

#ifndef GPD_Array_H
#define GPD_Array_H

//**** Includes ****
#include <vector>
#include <cstdlib>
#include <cstdarg>
//**** Uses ****
using std::vector;
namespace GPD {

template<typena me T_, int NDclass ArrayBase: public vector<T_{
public:// Defs
typedef typename vector<T_>::ite rator IterArr;

public:// Uses
using vector<T_>::beg in;
using vector<T_>::end ;
using vector<T_>::siz e;

public:// Constructors
ArrayBase(): vector<T_>() {}
ArrayBase(int Len): vector<T_>(Len) {}
ArrayBase(const ArrayBase& a): vector<T_>(a) {(*this) = a;}

public:// Destructor
virtual ~ArrayBase() {}

public:// Functions
int Dim(int N) const {return int(dim[N]);}
ArrayBase& operator=(const ArrayBase& a) {
vector<T_>::ope rator=(a);
copy(&a.dim[0], &a.dim[ND], &dim[0]);
copy(&a.sizeDim[0], &a.sizeDim[ND], &sizeDim[0]);
iData = begin();
return (*this);
}

protected:// Variables
size_t dim[ND];
size_t sizeDim[ND];
IterArr iData;
};

template<typena me T_, int NDclass SubArr {
private:// Defs
typedef typename ArrayBase<T_, ND>::iterator IterArr;

public:// Constructors
SubArr<T_, ND>(const IterArr IStart, const size_t* ArrSizeDim):
sizeDim(ArrSize Dim), iElem(IStart) {}

public:// Functions
SubArr<T_, ND-1 operator[](size_t K) const {
return SubArr<T_, ND-1>((iElem+K*siz eDim[0]),
(sizeDim+1));
}
private:// Variables
const size_t* sizeDim;
IterArr iElem;
};
template<typena me T_class SubArr<T_, 1{
private:// Defs
typedef typename ArrayBase<T_, 1>::iterator IterArr;

public:// Constructors
SubArr(const IterArr IStart, const size_t* ArrSizeDim):
sizeDim(ArrSize Dim), iElem(IStart) {}

public:// Functions
T_& operator[](size_t K) const {return (*(iElem+K));}

private:// Variables
const size_t* sizeDim;
IterArr iElem;
};
template<typena me T_, int ND=1class Array: public ArrayBase<T_, ND{
public:// Defs
typedef typename Array<T_, ND>::iterator IterArr;

public:// Uses
using vector<T_>::beg in;
using vector<T_>::end ;
using vector<T_>::siz e;
using ArrayBase<T_, ND>::sizeDim;
using ArrayBase<T_, ND>::dim;
using ArrayBase<T_, ND>::iData;

public:// Constructors
Array<T_, ND>(): ArrayBase<T_, ND>() {}
Array<T_, ND>(const Array<T_, ND>& a): ArrayBase<T_, ND>(a) {}
Array<T_, ND>(size_t D1, ...) {
// Initialize array dimensions and compute total array size
va_list ListDims;
va_start(ListDi ms, D1);
sizeDim[0] = 1;
for (int N = 0; N < ND; N++) {
dim[N] = (N ? va_arg(ListDims , int) : D1);
sizeDim[0] *= dim[N];
}
va_end(ListDims );

// Initialize array subspace sizes
for (int N = 1; N < ND; N++) {
sizeDim[N] = sizeDim[N-1] / dim[N-1];
}

// Allocate memory for data array
resize(sizeDim[0]);
iData = begin();
}

public:// Functions
void clear() const {return vector<T_>::cle ar();}
int Size() const {return int(size());}

void Resize(size_t D1, ...) {
// Initialize array dimensions and compute total size
va_list ListDims;
va_start(ListDi ms, D1);
sizeDim[0] = 1;
for (int N = 0; N < ND; N++) {
dim[N] = (N ? va_arg(ListDims , int) : D1);
sizeDim[0] *= dim[N];
}
va_end(ListDims );

// Initialize array subspace sizes
for (int N = 1; N < ND; N++) {
sizeDim[N] = sizeDim[N-1] / dim[N-1];
}

// Allocate memory for data array
resize(sizeDim[0]);
iData = begin();
}

void Clear() {
clear();
for (int N = 1; N < ND; N++) {
sizeDim[N] = 0;
}
}

void Fill(const T_& val) {
for (IterArr IT = begin(); IT < end(); IT++) {
(*IT) = val;
}
}

public:// Functions
SubArr<T_, ND-1 operator[](size_t K) const {
return SubArr<T_, ND-1>((iData+K*siz eDim[1]),
(sizeDim+2));
}
};
template<typena me T_class Array<T_, 1>: public ArrayBase<T_, 1{
public:// Constructors
Array(): ArrayBase<T_, 1>() {}
Array(size_t Len): ArrayBase<T_, 1>(Len) {}
Array(const ArrayBase<T_, 1>& a): ArrayBase<T_, 1>(a) {}
};

}

#endif
//=============== ========= Test Program =============== =============== =

//**** Includes****
#include "Array.h"
#include <iostream>
//**** Uses ****
using std::cout;
using std::endl;
using GPD::Array;
int main() {
cout << "Start Array Test: " << endl;
Array<float,3a( 3,2,5);
for (int N0 = 0; N0 < a.Dim(0); N0++) {
for (int N1 = 0; N1 < a.Dim(1); N1++) {
for (int N2 = 0; N2 < a.Dim(2); N2++) {
a[N0][N1][N2] = 100*(N0+1) + 10*(N1+1) + (N2+1);
cout << a[N0][N1][N2] << endl;
}
}
}
return 0;
}
Mar 3 '07 #3
John Harrison wrote:
JoeC wrote:
>I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}
You might want to put that into a class with an overloaded operator():

int & operator( std::size_t row, std::size_t col ) {
return ( the_data [ col_size * row + col ] );
}

and you could add a const version too:

int const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ col_size * row + col ] );
}

You also might make the member the_data a std::vector<int >. In that case,
you would not even have to take care of your own assignment operator, copy
constructor and destructor. E.g:. [code not tested/compiled]

template <typename T>
class array2d {

std::size_t the_row_size;
std::size_t the_col_size;
std::vector<Tth e_data;

public:

array2d ( std::size_t r, std::size_t c, T const & val = T() )
: the_row_size ( r )
, the_col_size ( c )
, the_data ( the_row_size * the_col_size, val )
{}

T & operator( std::size_t row, std::size_t col ) {
return ( the_data [ the_col_size * row + col ] );
}

T const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ the_col_size * row + col ] );
}

std::size_t row_size ( void ) const {
return ( the_row_size );
}

std::size_t col_size ( void ) const {
return ( the_col_size );
}

};
>>

That would work. There are other lots of other approaches.

The classic is (and simplest) to use double pointers

space** spaces = new space*[width];
for (int i = 0; i < width; ++i)
spaces[i] = new space[length];

then you can just say

space[i][j] = whatever;

and when you want to free the memory, do the reverse

for (int i = 0; i < width; ++i)
delete[] spaces[i];
delete[] spaces;
I doubt that this is the _simplest_ approach. Once you want an exception
safe version, you will find that it is not that simple at all. Any new in
the for-loop might throw. In order not to leak, you need to keep track of
that and delete the previously allocated rows. The idea of the OP is much
easier to implement properly.
Best

Kai-Uwe Bux
Mar 3 '07 #4
On Mar 3, 2:31 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
JoeC wrote:
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.
One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:
void place(int x, int y){spaces[x*len+y;}

That would work. There are other lots of other approaches.

The classic is (and simplest) to use double pointers

space** spaces = new space*[width];
for (int i = 0; i < width; ++i)
spaces[i] = new space[length];

then you can just say

space[i][j] = whatever;

and when you want to free the memory, do the reverse

for (int i = 0; i < width; ++i)
delete[] spaces[i];
delete[] spaces;

john
Thanks I never thought of that or seen that as an example. I will see
if my way works but thanks for idea.

Mar 3 '07 #5
On 3 Mar, 21:53, Doyle Rhynard <drhyn...@comca st.netwrote:
//=============== ========= Array.h =============== =============== =

#ifndef GPD_Array_H
#define GPD_Array_H

//**** Includes ****
#include <vector>
#include <cstdlib>
#include <cstdarg>

//**** Uses ****
using std::vector;
That's not a good thing to have in the header. Remove it and fully
qualify vector (i.e. type std::vector) throughout the header.

<snip the rest of Array.h>
//=============== ========= Test Program =============== =============== =

//**** Includes****
#include "Array.h"
With your header above, your program (and the OP's program if he uses
your code, and every other program that ever uses your code) now has
using std::vector forced upon it. That entirely defeats the purpose of
vector being in the std namespace. Using declarations and directives
have pros and cons that must be considered on a case by case basis. To
have the decision forced upon you, particularly by a such a generic
concept as a multi-dimensional array that has potential uses far
beyond the program it was originally created for, is a Bad Thing. And
it can easily be avoided by the one-off cost of a little extra typing
up front by fully qualifying the names in the header.

Gavin Deane

Mar 3 '07 #6
Kai-Uwe Bux wrote:
John Harrison wrote:
>JoeC wrote:
>>I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}

You might want to put that into a class with an overloaded operator():

int & operator( std::size_t row, std::size_t col ) {
return ( the_data [ col_size * row + col ] );
}

and you could add a const version too:

int const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ col_size * row + col ] );
}

You also might make the member the_data a std::vector<int >. In that case,
you would not even have to take care of your own assignment operator, copy
constructor and destructor. E.g:. [code not tested/compiled]

template <typename T>
class array2d {

std::size_t the_row_size;
std::size_t the_col_size;
std::vector<Tth e_data;

public:

array2d ( std::size_t r, std::size_t c, T const & val = T() )
: the_row_size ( r )
, the_col_size ( c )
, the_data ( the_row_size * the_col_size, val )
{}

T & operator( std::size_t row, std::size_t col ) {
return ( the_data [ the_col_size * row + col ] );
}

T const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ the_col_size * row + col ] );
}

std::size_t row_size ( void ) const {
return ( the_row_size );
}

std::size_t col_size ( void ) const {
return ( the_col_size );
}

};
Kai, you missed a pair of parens on both.

T& operator()( std::size_t row, std::size_t col );
T const & operator()( std::size_t row, std::size_t col ) const;
Mar 4 '07 #7

red floyd wrote:
Kai-Uwe Bux wrote:
John Harrison wrote:
JoeC wrote:
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}
You might want to put that into a class with an overloaded operator():

int & operator( std::size_t row, std::size_t col ) {
return ( the_data [ col_size * row + col ] );
}

and you could add a const version too:

int const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ col_size * row + col ] );
}

You also might make the member the_data a std::vector<int >. In that case,
you would not even have to take care of your own assignment operator, copy
constructor and destructor. E.g:. [code not tested/compiled]

template <typename T>
class array2d {

std::size_t the_row_size;
std::size_t the_col_size;
std::vector<Tth e_data;

public:

array2d ( std::size_t r, std::size_t c, T const & val = T() )
: the_row_size ( r )
, the_col_size ( c )
, the_data ( the_row_size * the_col_size, val )
{}

T & operator( std::size_t row, std::size_t col ) {
return ( the_data [ the_col_size * row + col ] );
}

T const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ the_col_size * row + col ] );
}

std::size_t row_size ( void ) const {
return ( the_row_size );
}

std::size_t col_size ( void ) const {
return ( the_col_size );
}

};
Kai, you missed a pair of parens on both.

T& operator()( std::size_t row, std::size_t col );
T const & operator()( std::size_t row, std::size_t col ) const;
Should you just use the valarray and slices and gslices?

I wonder.

Ross

--
Finlayson Consulting

Mar 4 '07 #8
red floyd wrote:
Kai-Uwe Bux wrote:
>John Harrison wrote:
>>JoeC wrote:
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.

One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:

void place(int x, int y){spaces[x*len+y;}

You might want to put that into a class with an overloaded operator():

int & operator( std::size_t row, std::size_t col ) {
return ( the_data [ col_size * row + col ] );
}

and you could add a const version too:

int const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ col_size * row + col ] );
}

You also might make the member the_data a std::vector<int >. In that case,
you would not even have to take care of your own assignment operator,
copy constructor and destructor. E.g:. [code not tested/compiled]

template <typename T>
class array2d {

std::size_t the_row_size;
std::size_t the_col_size;
std::vector<Tth e_data;

public:

array2d ( std::size_t r, std::size_t c, T const & val = T() )
: the_row_size ( r )
, the_col_size ( c )
, the_data ( the_row_size * the_col_size, val )
{}

T & operator( std::size_t row, std::size_t col ) {
return ( the_data [ the_col_size * row + col ] );
}

T const & operator( std::size_t row, std::size_t col ) const {
return ( the_data [ the_col_size * row + col ] );
}

std::size_t row_size ( void ) const {
return ( the_row_size );
}

std::size_t col_size ( void ) const {
return ( the_col_size );
}

};
Kai, you missed a pair of parens on both.

T& operator()( std::size_t row, std::size_t col );
T const & operator()( std::size_t row, std::size_t col ) const;
Oops, thanks.

I also missed the asserts that should be there:

T & operator() ( std::size_t row, std::size_t col ) {
assert( row < row_size() );
assert( col < col_size() );
return ( the_data [ the_col_size * row + col ] );
}

T const & operator() ( std::size_t row, std::size_t col ) const {
assert( row < row_size() );
assert( col < col_size() );
return ( the_data [ the_col_size * row + col ] );
}

And, of course, there should be some typedefs like value_type, reference,
const_reference , size_type. So what about:

struct array2d {

typedef typename std::vector<T>: :size_type size_type;
typedef typename std::vector<T>: :value_type value_type;
typedef typename std::vector<T>: :reference reference;
typedef typename std::vector<T>: :const_referenc e const_reference ;

private:

size_type the_row_size;
size_type the_col_size;
std::vector<Tth e_data;

size_type pos ( size_type row, size_type col ) {
assert( row < row_size() );
assert( col < col_size() );
return ( the_col_size * row + col );
}

public:

array2d ( size_type r, size_type c, const_reference val = T() )
: the_row_size ( r )
, the_col_size ( c )
, the_data ( the_row_size * the_col_size, val )
{
assert( the_row_size < size_type(-1) / the_col_size );
}

reference operator() ( size_type row, size_type col ) {
return ( the_data [ pos( row, col ) ] );
}

const_reference operator() ( size_type row, size_type col ) const {
return ( the_data [ pos( row, col ) ] );
}

size_type row_size ( void ) const {
return ( the_row_size );
}

size_type col_size ( void ) const {
return ( the_col_size );
}

};

The assert in the constructor is a little too strict.
Best

Kai-Uwe Bux

Mar 4 '07 #9
On Mar 3, 4:53 pm, Doyle Rhynard <drhyn...@comca st.netwrote:
I am crating a new version of my map game and my map will be a 2d
array. I had problems trying to create a 2d array dynamically, in
fact C++ won't let me do it. My question is how to create the size of
array I need at run time without using too much memory or going over
the allotted size if I choose to use this object for a different
game.
One idea I have is to create space * spaces = new space[len*with];
then have all my accessors just convert x and y to:
void place(int x, int y){spaces[x*len+y;}

I am getting rather tired of complaints about not being able to do 2-D arrays.

Here is a multidimensiona l array class derived from std::vector that I wrote and
have used for many years. It is not optimal, but is close enough for all but the
most time intensive programs.

If this not sufficient, the I would suggest using the Boost::multiarr ay.

//=============== ========= Array.h =============== =============== =

#ifndef GPD_Array_H
#define GPD_Array_H

//**** Includes ****
#include <vector>
#include <cstdlib>
#include <cstdarg>

//**** Uses ****
using std::vector;

namespace GPD {

template<typena me T_, int NDclass ArrayBase: public vector<T_{
public:// Defs
typedef typename vector<T_>::ite rator IterArr;

public:// Uses
using vector<T_>::beg in;
using vector<T_>::end ;
using vector<T_>::siz e;

public:// Constructors
ArrayBase(): vector<T_>() {}
ArrayBase(int Len): vector<T_>(Len) {}
ArrayBase(const ArrayBase& a): vector<T_>(a) {(*this) = a;}

public:// Destructor
virtual ~ArrayBase() {}

public:// Functions
int Dim(int N) const {return int(dim[N]);}
ArrayBase& operator=(const ArrayBase& a) {
vector<T_>::ope rator=(a);
copy(&a.dim[0], &a.dim[ND], &dim[0]);
copy(&a.sizeDim[0], &a.sizeDim[ND], &sizeDim[0]);
iData = begin();
return (*this);
}

protected:// Variables
size_t dim[ND];
size_t sizeDim[ND];
IterArr iData;
};

template<typena me T_, int NDclass SubArr {
private:// Defs
typedef typename ArrayBase<T_, ND>::iterator IterArr;

public:// Constructors
SubArr<T_, ND>(const IterArr IStart, const size_t* ArrSizeDim):
sizeDim(ArrSize Dim), iElem(IStart) {}

public:// Functions
SubArr<T_, ND-1 operator[](size_t K) const {
return SubArr<T_, ND-1>((iElem+K*siz eDim[0]),
(sizeDim+1));
}
private:// Variables
const size_t* sizeDim;
IterArr iElem;
};

template<typena me T_class SubArr<T_, 1{
private:// Defs
typedef typename ArrayBase<T_, 1>::iterator IterArr;

public:// Constructors
SubArr(const IterArr IStart, const size_t* ArrSizeDim):
sizeDim(ArrSize Dim), iElem(IStart) {}

public:// Functions
T_& operator[](size_t K) const {return (*(iElem+K));}

private:// Variables
const size_t* sizeDim;
IterArr iElem;
};

template<typena me T_, int ND=1class Array: public ArrayBase<T_, ND{
public:// Defs
typedef typename Array<T_, ND>::iterator IterArr;

public:// Uses
using vector<T_>::beg in;
using vector<T_>::end ;
using vector<T_>::siz e;
using ArrayBase<T_, ND>::sizeDim;
using ArrayBase<T_, ND>::dim;
using ArrayBase<T_, ND>::iData;

public:// Constructors
Array<T_, ND>(): ArrayBase<T_, ND>() {}
Array<T_, ND>(const Array<T_, ND>& a): ArrayBase<T_, ND>(a) {}
Array<T_, ND>(size_t D1, ...) {
// Initialize array dimensions and compute total array size
va_list ListDims;
va_start(ListDi ms, D1);
sizeDim[0] = 1;
for (int N = 0; N < ND; N++) {
dim[N] = (N ? va_arg(ListDims , int) : D1);
sizeDim[0] *= dim[N];
}
va_end(ListDims );

// Initialize array subspace sizes
for (int N = 1; N < ND; N++) {
sizeDim[N] = sizeDim[N-1] / dim[N-1];
}

// Allocate memory for data array
resize(sizeDim[0]);
iData = begin();
}

public:// Functions
void clear() const {return vector<T_>::cle ar();}
int Size() const {return int(size());}

void Resize(size_t D1, ...) {
// Initialize array dimensions and compute total size
va_list ListDims;
va_start(ListDi ms, D1);
sizeDim[0] = 1;
for (int N = 0; N < ND; N++) {
dim[N] = (N ? va_arg(ListDims , int) : D1);
sizeDim[0] *= dim[N];
}
va_end(ListDims );

// Initialize array subspace sizes
for (int N = 1; N < ND; N++) {
sizeDim[N] = sizeDim[N-1] / dim[N-1];
}

// Allocate memory for data array
resize(sizeDim[0]);
iData = begin();
}

void Clear() {
clear();
for (int N = 1; N < ND; N++) {
sizeDim[N] = 0;
}
}

void Fill(const T_& val) {
for (IterArr IT = begin(); IT < end(); IT++) {
(*IT) = val;
}
}

public:// Functions
SubArr<T_, ND-1 operator[](size_t K) const {
return SubArr<T_, ND-1>((iData+K*siz eDim[1]),
(sizeDim+2));
}
};

template<typena me T_class Array<T_, 1>: public ArrayBase<T_, 1{
public:// Constructors
Array(): ArrayBase<T_, 1>() {}
Array(size_t Len): ArrayBase<T_, 1>(Len) {}
Array(const ArrayBase<T_, 1>& a): ArrayBase<T_, 1>(a) {}
};

}

#endif

//=============== ========= Test Program =============== =============== =

//**** Includes****
#include "Array.h"
#include <iostream>

//**** Uses ****
using std::cout;
using std::endl;
using GPD::Array;

int main() {
cout << "Start Array Test: " << endl;
Array<float,3a( 3,2,5);
for (int N0 = 0; N0 < a.Dim(0); N0++) {
for (int N1 = 0; N1 < a.Dim(1); N1++) {
for (int N2 = 0; N2 < a.Dim(2); N2++) {
a[N0][N1][N2] = 100*(N0+1) + 10*(N1+1) + (N2+1);
cout << a[N0][N1][N2] << endl;
}
}
}
return 0;

}
Thanks, that is a bit more complex than I need.

Mar 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3672
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
6
2973
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
12
6240
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE *********************************** set objRS = GetMyRecordSet() i=1 objRS.MoveFirst
3
1938
by: Dan | last post by:
Is it possible to dynamically create and populate properties? Below is my class. I would like to define properties for each of the items in my two arrays without actually defining each one. (There are over 100 in total). I would to have them set up the same as i have "form_id" below, but im not sure what the syntax to do it is (without actually typing each one). ' declare form_id private form_id
0
2316
by: skm376 | last post by:
I am a little confused as to how I need to allocate memory for a dynamic array inside a struct. Here is the struct that I am using: typedef struct command_t *commandPtr; typedef struct command_t { char* name; int argc; char* argv; } commandT;
9
2945
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
2
2109
by: headware | last post by:
Do dynamic arrays declared using ReDim have to be freed? I assume that if it's an array of dynamically created objects (e.g. Scripting.Dictionary), each one of those objects will have to be set to Nothing in a for loop, but what about the array itself? Or what if the array is dynamic but it just consists of integers or strings, not dynamically allocated objects? Does it have to be set to Nothing? Thanks, Dave
7
3109
by: alternative451 | last post by:
Hi, I have just one question : how to know the size of an array, i have un little program, i use first static, ant i can use sizeof() to know the size, but when i put it as paremeter in the function, size of return "1". ex int tab; printf("%d",sizeof(tab)/sizeof(int)); // print 10 int length(int *tab)
4
5416
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am putting in arrays.
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.