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
  Howard Moscovitz Chez Mosc - No show tonight - sorry
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
[SOLVED] Working with Arrays and functions
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
agkbill



Joined: Feb 15, 2007
Posts: 11
Location: Uppsala Sweden

PostPosted: Sun Dec 12, 2010 10:44 am    Post subject:  [SOLVED] Working with Arrays and functions Reply with quote  Mark this post and the followings unread

Hi,

I am rather new to programming but the possibilities with chucK made me interested.

But the more I explore I see that my code increase get exponentially harder to read and understand.

well, I guess there are many ways to fix that functions, objects but it not easy to understand.

I started with functions and managed to get it to work with int.
But I would like to use arrays. Can I use a array as input and a int as output?


What I want to make a function of is a couple of lines that from two arrays calculate a int. One array is fixed and the outer is variable.

Talking about arrays, is it possible to do calculations on whole arrays?


Thank you,

All the best!

/Christer

Last edited by agkbill on Mon Dec 13, 2010 2:20 pm; edited 1 time in total
Back to top
View user's profile Send private message
vrachnasormora



Joined: Mar 22, 2007
Posts: 42
Location: Preveza,Greece

PostPosted: Mon Dec 13, 2010 6:54 am    Post subject: Reply with quote  Mark this post and the followings unread

Hi Christer,
If I got your problem correctly, you might need something along these lines?:

Code:
[ 1, 2, 3 ] @=> int fixed[];

fun int two_arrays( int variable[] )
{
    int result;
   
    //I don't know what kind of calculation you may want to perform
    // on these arrays so I'll go on with a simple addition of all
    // elements from both arrays
   
    for ( int i; i < fixed.size(); i++ )
    {
        fixed[i] +=> result;
    }
    for ( int i; i < variable.size(); i++ )
    {
        variable[i] +=> result;
    }
   
    return result;
}


If you need to correct me, please don't hesitate to reply with code

Cheers,
Andreas
Back to top
View user's profile Send private message
agkbill



Joined: Feb 15, 2007
Posts: 11
Location: Uppsala Sweden

PostPosted: Mon Dec 13, 2010 1:33 pm    Post subject: Reply with quote  Mark this post and the followings unread

Dear vrachnasormora,

You got my problem correct, thank you for helping!

I am writing a app for monome (www.monome.org) and the array is value in different positions, but from them I need to calculate a int that are used to control the LED on the monome.

To test your example I did.

Code:
[ 1, 2, 3 ] @=> int fixed[];
[ 5, 5, 5 ] @=> int variable[];




fun int two_arrays( int variable[] )
{
    int result;
   
    //I don't know what kind of calculation you may want to perform
    // on these arrays so I'll go on with a simple addition of all
    // elements from both arrays
   
    for ( int i; i <fixed> result;
    }
    for ( int i; i <variable> result;
    }
   
    return result;
}

two_arrays( variable ) => int out;
<<<out>>>;



It took me some time to understand how to get array data into the function.

I tried:
two_arrays( variable[] ) => int out;
in many different forms until I discovered that I should not have the [] only the name of the array.

But now I will be able to keep on working on my app.

Thank you!

All the best,
/Christer
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 [3 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
Niio2

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