Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multidimensional array class

Reply
Thread Tools

multidimensional array class

 
 
Mike
Guest
Posts: n/a
 
      04-19-2010
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[i] = 32+rand()%223;
i++;
}

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

droplet::~droplet(){
// for(int i=0;i<16;i++) delete drops[i];//destorying droplets
// delete[] drops;//destroying droplet pointer array
}
 
Reply With Quote
 
 
 
 
JoeC
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 4:05*am, Mike <michael.sg...@gmail.com> 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[i] = 32+rand()%223;
> * * * * i++;
> * * * *}
>
> * * * *// pos.Y=y;
> * * * * speed=spd;
> * * }
>
> * * droplet::~droplet(){
> *// * for(int i=0;i<16;i++) delete drops[i];//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.

 
Reply With Quote
 
 
 
 
JoeC
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 4:05*am, Mike <michael.sg...@gmail.com> 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[i] = 32+rand()%223;
> * * * * i++;
> * * * *}
>
> * * * *// pos.Y=y;
> * * * * speed=spd;
> * * }
>
> * * droplet::~droplet(){
> *// * for(int i=0;i<16;i++) delete drops[i];//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;
}

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      04-20-2010
I tried so:

droplet::droplet(int x, int y, int spd){
int i=0;
pos = new int[x*y];
while(i<16){
trail[i] = 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
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      04-21-2010
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[i] = 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[i]=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

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      04-21-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Binding Multidimensional Array to DataGrid epigram ASP .Net 1 07-15-2005 11:02 PM
Multidimensional Dyamic Array in class undbund C++ 1 09-08-2004 12:39 PM
Returning a multidimensional array. Ben Java 8 12-18-2003 08:48 AM
slice of multidimensional array Dave Bazell Perl 2 07-24-2003 11:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57