Page 1 of 2

[C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 05, 2010 2:03 pm
by acerookie1
im trying to make a 1D array of this class for a particle engine:

Code: Select all

class Particle
{

    public:
    int life;
    int x,y,max_life;
    Particle(int X, int Y, int LIFE,string &image_name);
    bool kill();
};
how do i set values to it. ex: im going to use an emitter class that will handle the setting of the values of the particles. i see people looping the array for 2d and 3d but idk what im really doing.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 05, 2010 2:20 pm
by mv2112
Do something like this:

Code: Select all

class Emitter
{
public:
void Set(int bla, bool bla_, string  bla__);
private:
static const int NUM=10000;
Particle[NUM] Particles;
};

void Emitter::Set(int bla, bool bla_, string bla__)
{
    for(int i=0;i<NUM;i++)
    {
    Particles[i].Set(bla,bla_,bla__);
    }
}
You might also want to add a update function in the particles so you can do stuff like this:

Code: Select all

void Emitter::Update()
{
   for(int i=0;i<NUM;i++)
   {
        Particles[i].Update(); //move particles
   }
}

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 05, 2010 2:24 pm
by acerookie1
[quote="mv2112"]Do something like this:

Code: Select all

class Emitter
{
public:
void Set(int bla, bool bla_, string  bla__);
private:
static const int NUM=10000;
Particle[NUM] Particles;
};

void Emitter::Set(int bla, bool bla_, string bla__)
{
    for(int i=0;i<NUM;i++)
    {
    Particles[i].Set(bla,bla_,bla__);
    }
}
\
are you sure thats dynamic? y arent u using new or delete?

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 05, 2010 4:08 pm
by xiphirx
acerookie1 wrote:
mv2112 wrote:Do something like this:

Code: Select all

class Emitter
{
public:
void Set(int bla, bool bla_, string  bla__);
private:
static const int NUM=10000;
Particle[NUM] Particles;
};

void Emitter::Set(int bla, bool bla_, string bla__)
{
    for(int i=0;i<NUM;i++)
    {
    Particles[i].Set(bla,bla_,bla__);
    }
}
\
are you sure thats dynamic? y arent u using new or delete?

The only reason you should make it dynamic is if you are allowing the user to change the max amount of particles. Otherwise you should have a set limit.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 05, 2010 4:54 pm
by mv2112
xiphirx wrote: The only reason you should make it dynamic is if you are allowing the user to change the max amount of particles. Otherwise you should have a set limit.
Ya, creating and deleting particles after each cycle is very slow and inefficient. Like xiphirx said, the only reason you'd want it dynamic is if you want to change the particle limit.

Also, i'd like to change the code i posted:

Code: Select all

class Emitter
{
public:
Emitter();
~Emitter();
private:
static const int NUM=10000;
Particle * Particles;
};

Emitter::Emitter()
{
Particles=new Particle[NUM];
}

Emitter::~Emitter()
{
delete [] Particles;
}
Having too many particles that arnt allocated at runtime can cause a crash.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Jul 12, 2010 11:05 pm
by acerookie1
mv2112 wrote:
xiphirx wrote: The only reason you should make it dynamic is if you are allowing the user to change the max amount of particles. Otherwise you should have a set limit.
Ya, creating and deleting particles after each cycle is very slow and inefficient. Like xiphirx said, the only reason you'd want it dynamic is if you want to change the particle limit.

Also, i'd like to change the code i posted:

Code: Select all

class Emitter
{
public:
Emitter();
~Emitter();
private:
static const int NUM=10000;
Particle * Particles;
};

Emitter::Emitter()
{
Particles=new Particle[NUM];
}

Emitter::~Emitter()
{
delete [] Particles;
}
Having too many particles that arnt allocated at runtime can cause a crash.
yeah i fixed this. made it dynamic. idk what this was.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Fri Oct 22, 2010 4:34 am
by MacJordan
Following is way you can define array in C++ :

One dimensional array :

int arrrayone[] = {1,2,3,4,5};

here you can define infinite value in this array because we have not define the size of array .

i you would like to define the size of array then consider,

int arrayone[2]= {1,2};

it will accepts only 2 parameter. it will not exceed from 2 other wise it will show an error

two dimensional array :

int arraytwo[ ] [ ] = {(1,2),(3,4),(5,6)};

first parantheses define the row and second one define column or array.


three dimensional array :

int arraythree[ ] [ ] [ ] = {(1,2,3),(4,5,6)};

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sat Oct 23, 2010 7:54 pm
by acerookie1
MacJordan wrote:Following is way you can define array in C++ :

One dimensional array :

int arrrayone[] = {1,2,3,4,5};

here you can define infinite value in this array because we have not define the size of array .

i you would like to define the size of array then consider,

int arrayone[2]= {1,2};

it will accepts only 2 parameter. it will not exceed from 2 other wise it will show an error

two dimensional array :

int arraytwo[ ] [ ] = {(1,2),(3,4),(5,6)};

first parantheses define the row and second one define column or array.


three dimensional array :

int arraythree[ ] [ ] [ ] = {(1,2,3),(4,5,6)};
thanx again

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sun Oct 24, 2010 1:39 pm
by Ginto8
MacJordan wrote:Following is way you can define array in C++ :

One dimensional array :

int arrrayone[] = {1,2,3,4,5};

here you can define infinite value in this array because we have not define the size of array .

i you would like to define the size of array then consider,

int arrayone[2]= {1,2};

it will accepts only 2 parameter. it will not exceed from 2 other wise it will show an error

two dimensional array :

int arraytwo[ ] [ ] = {(1,2),(3,4),(5,6)};

first parantheses define the row and second one define column or array.


three dimensional array :

int arraythree[ ] [ ] [ ] = {(1,2,3),(4,5,6)};
okay, for your 1D array, that declaration works. However, for 2D and 3D arrays, you're completely wrong. Even if you could do it that way, there are 2 things wrong: 1) you'd need {} around the 3D array, and you'd use {} instead of (). However, you can't, as this will give you an error saying "array type has incomplete element type". You can declare your 1D array like:

Code: Select all

int array[] = { /* stuff */ };
but for 2D and 3D arrays, you'd have to populate it another way, ie:

Code: Select all

int array[3][2];
for(int y=0;y<3;++y)
    for(int x=0;x<2;++x)
        array[y][x] = 5;
You, MacJordan, appear to think you know how to program, but you have repeatedly shown that you do not.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sun Oct 24, 2010 6:38 pm
by wearymemory
Ginto8 wrote:okay, for your 1D array, that declaration works. However, for 2D and 3D arrays, you're completely wrong. Even if you could do it that way, there are 2 things wrong: 1) you'd need {} around the 3D array, and you'd use {} instead of (). However, you can't, as this will give you an error saying "array type has incomplete element type". You can declare your 1D array like:

Code: Select all

int array[] = { /* stuff */ };
but for 2D and 3D arrays, you'd have to populate it another way, ie:

Code: Select all

int array[3][2];
for(int y=0;y<3;++y)
    for(int x=0;x<2;++x)
        array[y][x] = 5;
Indeed, MacJordan's code isn't syntactically correct; however, the following code is:

Code: Select all

int a[3][2] = {{5,5},{5,5},{5,5}};
MacJordan presented ways to initialize multidimensional arrays, and although they were incorrect, they warranted further research into the topic.
Ginto8 wrote:You, MacJordan, appear to think you know how to program, but you have repeatedly shown that you do not.
You, Ginto8, appear to think you know everything, but you have repeatedly shown that you do not.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sun Oct 24, 2010 8:37 pm
by Ginto8
wearymemory wrote:
Ginto8 wrote:You, MacJordan, appear to think you know how to program, but you have repeatedly shown that you do not.
You, Ginto8, appear to think you know everything, but you have repeatedly shown that you do not.
You, wearymemory, appear to think you aren't an asshole, but you have repeatedly shown that you are not.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sun Oct 24, 2010 10:09 pm
by dandymcgee
Ginto8 wrote: You, wearymemory, appear to think you aren't an asshole, but you have repeatedly shown that you are not.
Lol. You just contradicted yourself.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Sun Oct 24, 2010 10:24 pm
by Ginto8
dandymcgee wrote:
Ginto8 wrote: You, wearymemory, appear to think you aren't an asshole, but you have repeatedly shown that you are not.
Lol. You just contradicted yourself.
lolwut.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Oct 25, 2010 9:16 am
by Van-B
Is 'Particles' a standard class in the library you guys are using? - I just expected to see the Particles data footprint being set, which I'm thinking might be of use.

I don't want to make more of a fool of myself than is necessary, so can someone clear that up please, should someone make an example using a struct or something to set the data types.

Re: [C++] how to declare and use 1D, 2D, and 3D arrays

Posted: Mon Oct 25, 2010 1:13 pm
by GroundUpEngine
dandymcgee wrote:
Ginto8 wrote: You, wearymemory, appear to think you aren't an asshole, but you have repeatedly shown that you are not.
Lol. You just contradicted yourself.
:lol: