multidimensional array class

Discussion in 'C++' started by Mike, Apr 19, 2010.

  1. Mike

    Mike Guest

    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
    }
    Mike, Apr 19, 2010
    #1
    1. Advertising

  2. Mike

    JoeC Guest

    On Apr 19, 4:05 am, Mike <> wrote:
    > 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.
    JoeC, Apr 19, 2010
    #2
    1. Advertising

  3. Mike

    JoeC Guest

    On Apr 19, 4:05 am, Mike <> wrote:
    > 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;
    }
    JoeC, Apr 19, 2010
    #3
  4. Mike

    Mike Guest

    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
    Mike, Apr 20, 2010
    #4
  5. Mike

    Mike Guest

    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
    Mike, Apr 21, 2010
    #5
  6. Mike

    Mike Guest

    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
    Mike, Apr 21, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,061
  2. epigram
    Replies:
    1
    Views:
    10,775
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 16, 2005
  3. Ben
    Replies:
    8
    Views:
    12,559
    Eki Y. Baskoro
    Dec 18, 2003
  4. Huub
    Replies:
    6
    Views:
    596
  5. undbund
    Replies:
    1
    Views:
    445
    John Harrison
    Sep 8, 2004
Loading...

Share This Page