electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
Live streaming at radio.electro-music.com

  host / artist show at your time
  Jez Adventures in Sound
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
2d dynamic array
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
robertg



Joined: Oct 23, 2010
Posts: 4
Location: Fort Lauderdale, FL

PostPosted: Sat Mar 26, 2011 7:40 am    Post subject: 2d dynamic array
Subject description: 1 dim dynamic other fixed
Reply with quote  Mark this post and the followings unread

Hello

Trying to create a dynamic array structure which represents a record like structure of three variables. Basic idea is that increasing size of the first array dim adds a new row and the second dim represents vars of that row. The second dim doesn't grow in size.

a[1][3] - row
a[2][3] - next, etc.

I tried the various methods to resize arrays: size, << but I keep running into issues when assigning/ accessing elements. Is it possible to grow one dimension but keep the other dimension fixed? Are there specific guidelines when accessing the dynamic/ static dimensional array structure?

// code sample

// first row
int a[1][3];
<<< a.size() >>>; // this appears to return 1st dim size
<<< a[0].size() >>>; // this appears tor return 2nd dim size though syntax is not clear why would be the case

1 => a[0][0]; <<< a[0][0] >>>; //works
2 => a[0][1]; <<< a[0][1] >>>; // works
3 => a[0][2]; <<< a[0][2] >>>; // works

// second row
a.size() + 1 => a.size; // this appears to increase first dim size
<<< a.size() >>>;
<<< a[0].size() >>>;

4 => a[1][0]; <<< a[1][0] >>>; // Assignment errors start here after first dynamic row increase
5 => a[1][1]; <<< a[1][1] >>>;
6 => a[1][2]; <<< a[1][2] >>>;



Thanks - appreciated
Robert
Code:
Back to top
View user's profile Send private message
robertg



Joined: Oct 23, 2010
Posts: 4
Location: Fort Lauderdale, FL

PostPosted: Sun Mar 27, 2011 5:41 am    Post subject:
Subject description: problems with multi dim float arrays?
Reply with quote  Mark this post and the followings unread

Still working on this. I was able to get this to work with a multi dim int array but not (same code!) with a multi dim float array. Any assistance greatly appreciated!

// example 1 - works with dynamic multi dim int array
int a[0][0];

a << [1, 2, 3];

<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;

<<< "a[0][0]:", a[0][0] >>>;
<<< "a[0][1]:", a[0][1] >>>;
<<< "a[0][2]:", a[0][2] >>>;


a << [4, 5, 6];
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[1].size(): ", a[1].size() >>>;

<<< "a[1][0]:", a[1][0] >>>;
<<< "a[1][1]:", a[1][1] >>>;
<<< "a[1][2]:", a[1][2] >>>;


// example 2 - same thing crashes when type is changed to float

float a[0][0];

a << [1.0, 2.0, 3.0];


<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;

<<< "a[0][0]:", a[0][0] >>>;
<<< "a[0][1]:", a[0][1] >>>;
<<< "a[0][2]:", a[0][2] >>>;



// example 3 - this doesn't work either, it seems to only increase first array dim size but doesn't create slots for second dim

float a[1][3];

1.0 => a[0][0];
2.0 => a[0][1];
3.0 => a[0][2];

<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;

a.size() + 1 => a.size;


<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[1].size(): ", a[1].size() >>>; // this crashes

4.0 => a[1][0]; // these assignments crash
5.0 => a[1][1];
6.0 => a[1][2];





Thanks
Back to top
View user's profile Send private message
trzewiczek



Joined: Sep 24, 2010
Posts: 4
Location: Poland

PostPosted: Fri Apr 01, 2011 11:18 pm    Post subject:
Subject description: Funny thing with float array size
Reply with quote  Mark this post and the followings unread

Hi,

It looks like a bug in the language. I tried to push thing to the wall and started to play stupid stuff with those arrays. Take a look

Code:
int a[0][0];

a << 1;
a << 2;
a << 3;
a << 4;
a << 5;

<<< "a.size()", a.size() >>>;

for( 0 => int i; i < a.size(); i++ )
{
    <<< "a[", i, "] ::", a[i] >>>;
}


The output is:

Code:
a.size() 5   
a[ 0 ] :: 0x1 
a[ 1 ] :: 0x2
a[ 2 ] :: 0x3
a[ 3 ] :: 0x4
a[ 4 ] :: 0x5 


I know, I'm not fair, but... Wink

This makes funny thing - it seems to assigns new addresses in memory for each array position. This is kind of cool - no problem with that (thow, it crashes ChucK after using it and "why would you like to do that?" Wink).

What's funny for me is what happens when you change int to float in this code. The output is:

Code:
a.size() 10
a[ 0 ] :: 0x0                         
a[ 1 ] :: 0x3ff00000
a[ 2 ] :: 0x0
a[ 3 ] :: 0x40000000                         
a[ 4 ] :: 0x0                           
a[ 5 ] :: 0x40080000 
a[ 6 ] :: 0x0   
a[ 7 ] :: 0x40100000 
a[ 8 ] :: 0x0
a[ 9 ] :: 0x40140000


Array size is... 10!! OK - I'm not a computer sciencist, so maybe it ok, but for my uneducated soul it looks, like there is something wrong with float dynamic multidim arrays handling in the language.

I know it's not very useful, but you know.. just an interesting thing...

_________________
ChucK => now;
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 3708
Location: Sweden
Audio files: 270
G2 patch files: 96

PostPosted: Sat Apr 02, 2011 6:09 am    Post subject: Reply with quote  Mark this post and the followings unread

Yeah arrays work weird sometimes. I just take care to keep in mind that arrays are dodgy, and make sure I test my use of arrays thorougly before deciding that I can rely on my solutions.

In my world ChucK should have thrown a compilation error at your first assignment to a:

Code:
a << 1;


"Why are you trying to assign an integer value to an array? Stop doing that!"

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
trzewiczek



Joined: Sep 24, 2010
Posts: 4
Location: Poland

PostPosted: Sat Apr 02, 2011 10:00 am    Post subject: Reply with quote  Mark this post and the followings unread

With this:

Code:
int a[0];

a << 1;
a << 2;


it's quite ok for me. It' a shortcut for:

Code:
a.push( 1 );
a.push( 2 );


But then it comes to multidim arrays it's really weird!! You're pushing int where the array of ints should be pushed and no error is sent!

And btw I really don't like empty array declaration:

Code:
int a[0];  // zero length array?!


It looks ugly and for me is an example of a bad design style of ChucK.

There should be a special wiki pega with "Array's qeirdness" Wink

Best,
trzewiczek

_________________
ChucK => now;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use