Pages: [1]   Go Down
Author Topic: function of type integer array  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello!
i want to randomize an array of 8 integer numbers.
an elegant way would be a function from the type of an array of integer numbers which returns a randomized array.

in this case i could call the randomize function easily:
ExistingArray = RandomFunction;

i just right now do not remember how to do this.
i tried a declaration in the style of
Code:
int[8] randomize(int k){
  randomSeed(millis());
  for (int i=0; i<k; i++){
    randomize[i] = random(0,1);
  }
  return randomize;
}

but this is not quite correct smiley-wink
« Last Edit: June 10, 2007, 07:00:36 am by haesslich » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's probably easier to pass the array as a parameter into the function - any values you assign to it within the function will carry over to the calling function.  If you want to return an array from a function, you start needing to deal with allocation and deallocation of memory.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 267
dinosaur cork
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You might find something of use in this approach too.


http://www.arduino.cc/playground/Main/RandomHat
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
No OS is secure from a hot soldering iron.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would be very grateful if forum gurus could post a short piece of code that does send an array to a function, and then return a modified array back to the calling routine.  That is, similar to the code provided by haesslich (but which works correctly).

i.e. a piece of code like this but which has correct syntax:-

void(loop) {
...
int sourceValues[6] = {0,1,2,3,4,5};
...
FunctionWhichOperatesOnSourceValueArray(sourceValues);
...
}

int[6] FunctionWhichOperatesOnSourceValueArray(int[6]) {
byte counter = 0;
for (counter = 0; counter < 6; counter ++) {
  array(counter)++
  }
return array somehow
}


Regards,
TB
« Last Edit: June 24, 2007, 05:14:35 am by tonybarry » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't actually need to return the array from the function.  Just modify the one that got passed in, and the changes will be reflected in the calling function.

E.g.

Code:
int pins[] = { 2, 3, 4 };

void incrementArray(int arr[], int length)
{
  for (int i = 0; i < length; i++) {
    arr[i] = arr[i] + 1;
  }
}

void setup()
{
  incrementArray(pins, 3);
  // now pins is { 3, 4, 5 }
}

void loop()
{
  // ...
}
« Last Edit: June 25, 2007, 04:01:29 pm by mellis » Logged

Pages: [1]   Go Up
Jump to: