| Author |
Message |
agkbill
Joined: Feb 15, 2007 Posts: 11 Location: Uppsala Sweden
|
Posted: Sun Dec 12, 2010 10:44 am Post subject:
[SOLVED] Working with Arrays and functions |
 |
|
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
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Mon Dec 13, 2010 6:54 am Post subject:
|
 |
|
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
|
|
 |
agkbill
Joined: Feb 15, 2007 Posts: 11 Location: Uppsala Sweden
|
Posted: Mon Dec 13, 2010 1:33 pm Post subject:
|
 |
|
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
|
|
 |
|