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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

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

Post 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.
Image
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

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

Post 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
   }
}
acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

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

Post 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?
Image
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

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

Post 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.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

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

Post 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.
acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

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

Post 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.
Image
MacJordan
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Fri Oct 22, 2010 4:13 am

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

Post 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)};
acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

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

Post 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
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

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

Post 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.
Last edited by wearymemory on Mon Oct 25, 2010 6:33 pm, edited 1 time in total.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Van-B
Chaos Rift Regular
Chaos Rift Regular
Posts: 125
Joined: Tue Aug 10, 2010 7:17 am
Current Project: iPhone puzzle game
Favorite Gaming Platforms: All - Except Amiga
Programming Language of Choice: DBPro, ObjC++
Location: Scotland

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

Post 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.
Health, ammo.... and bacon and eggs.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post 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:
Post Reply