multidimensional array class

M

Mike

Hi
below how/where do i initialize the array? pos[4][4] = { x,y };
How can I then access and write to positions within the array?
Many thanks
Michael
---------
class droplet
{
private:
int pos[4][4];//location of drop head on screen
int trail[16];//information on all characters
int speed;//speed, 1, 2 or 3.


public:

droplet(int x, int y, int spd);
~droplet();

void draw();
void down();
};

droplet::droplet(int x, int y, int spd){
int i=0;
while(i<16){
// for (x=0; x<4; x++)
// for (y=0; y<4; y++)
// {
pos[4][4] = { x,y };

// }
trail = 32+rand()%223;
i++;
}

// pos.Y=y;
speed=spd;
}

droplet::~droplet(){
// for(int i=0;i<16;i++) delete drops;//destorying droplets
// delete[] drops;//destroying droplet pointer array
}
 
J

JoeC

Hi
   below how/where do i initialize the array?  pos[4][4] = { x,y };
How can I then access and write to positions within the array?
Many thanks
Michael
---------
    class droplet
    {
    private:
    int pos[4][4];//location of drop head on screen
        int trail[16];//information on all characters
    int speed;//speed, 1, 2 or 3.

    public:

        droplet(int x, int y, int spd);
        ~droplet();

        void draw();
        void down();
    };

    droplet::droplet(int x, int y, int spd){
    int i=0;
       while(i<16){
 //     for (x=0; x<4; x++)
  //            for (y=0; y<4; y++)
//              {
           pos[4][4] = { x,y };

//              }
        trail = 32+rand()%223;
        i++;
       }

       // pos.Y=y;
        speed=spd;
    }

    droplet::~droplet(){
 //   for(int i=0;i<16;i++) delete drops;//destorying droplets
 //   delete[] drops;//destroying droplet pointer array
    }


The way I like to do a 2d array is simulate one. You can get the size
of your array[acc*up].

Then to access the elements:

element(acc, up)

The formula is (down*number of elements across+ number accross.

array[dwn * pitch + acc] = ...

This way you can use the container you need an array or other
container.
 
J

JoeC

Hi
   below how/where do i initialize the array?  pos[4][4] = { x,y };
How can I then access and write to positions within the array?
Many thanks
Michael
---------
    class droplet
    {
    private:
    int pos[4][4];//location of drop head on screen
        int trail[16];//information on all characters
    int speed;//speed, 1, 2 or 3.

    public:

        droplet(int x, int y, int spd);
        ~droplet();

        void draw();
        void down();
    };

    droplet::droplet(int x, int y, int spd){
    int i=0;
       while(i<16){
 //     for (x=0; x<4; x++)
  //            for (y=0; y<4; y++)
//              {
           pos[4][4] = { x,y };

//              }
        trail = 32+rand()%223;
        i++;
       }

       // pos.Y=y;
        speed=spd;
    }

    droplet::~droplet(){
 //   for(int i=0;i<16;i++) delete drops;//destorying droplets
 //   delete[] drops;//destroying droplet pointer array
    }


class grid{

int acc;
int dwn;
type * grid;
};

create{

grid = new type [acc*dwn];

}

change (int ac, dw, num){

grid[dw*acc+ac] = num;
}
 
M

Mike

I tried so:

droplet::droplet(int x, int y, int spd){
int i=0;
pos = new int[x*y];
while(i<16){
trail = 32+rand()%223;
i++;
}
speed=spd;
}


but when I try to access y or x later in a function i get "not
declared"
if(drops[y]+speed>top
Thanks again
Michael
 
M

Mike

thanks so i try this for the constructor. But how would I access f.ex.
pos[2][1]?
I try something alike:
---------
int num=((rand()%4)+1)*50;
droplet **drops = new droplet * [num];//declare a pointer of a droplet
pointer and sets it to an array of droplet pointers

for(int i=0;i<num;i++)
drops = new droplet(rand()%left + 20,rand()%(top - 30),rand()
%2+1);//initializes each droplet pointer to a randomly placed droplet
with random speed

for(int i=0;i<num;i++)//runs through all drops
{
if(pos.Y+speed>PanelWindowTop - 30){pos.X=rand()%PanelWindowLeft + 20;
pos.Y=0;}else pos.Y+=speed;
for(int i=num;i>0;i--)
trail=trail[i-1];
trail[0] = 32+rand()%223;

opengl draw droplets()
}

Yes I try to create a class for a rain simulation. Later I also need
functions for drops to roll down. Maybe if more than one droplets have
the same x/y. And also wind coming in. No wind straight down, light
wind drops fall to the side and much wind drops move upwards. As the
airplane windscreen is rounded I might even need to integrate/simulate
a wind center point. But this is all for later, now I only try the
class droplet(top,left,speed).
Thanks again & cheers
Michael
 
M

Mike

ok now i try alike:

mytype *array;
array = new mytype[10*20]; //x*y

access via:

array[1*10+5] = 4;
int x = array[1*10+5]; //what is that? x or y?

Hmm I don't understand. How could I check x/y?
if array.y>200 or array.x<20 etc.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top