Pages: [1]   Go Down
Author Topic: Reverse order of Integer array.  (Read 1084 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello this is a noob programming question, but i just cant get it to work.
Im trying to take my int array named "buffer" and reverse the order of all numbers in it, and put it in a new array named "buffer2".
Any tips on how i can achieve this?
Thanks in advance.
Logged

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 489
Always learning!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int x = 0;
int y = 255; //CHANGE TO SIZE OF "BUFFER" ARRAY!

while(y){
  buffer[x] = buffer2[y];
  x++;
  y--;
}

Remember to change the size of 'y' to the size of 'buffer'.
Onions.
Logged

My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) smiley-grin

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

an inplace reverse
Code:
#define BUFFERSIZE 123
int buffer[BUFFERSIZE];

reverse()
{
  for (int i=0, j = BUFFERSIZE-1; i< BUFFERSIZE/2; i++, j--)
  {
    int temp = buffer[i];
    buffer[i] = buffer[j];
    buffer[j] = temp;
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Big thanks for the fast reply! Great support!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16513
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int x = 0;
int y = 255; //CHANGE TO SIZE OF "BUFFER" ARRAY!

while(y){
  buffer[x] = buffer2[y];
  x++;
  y--;
}

Remember to change the size of 'y' to the size of 'buffer'.
Onions.


So maybe use: int y = sizeof(buffer); ?

Logged

Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So maybe use: int y = sizeof(buffer); ?

Which of course only works if you're not passing the array as an argument to a function.
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So maybe use: int y = sizeof(buffer); ?

No, because 'sizeof' returns the number of bytes in the array, not the number of elements. Since an 'int' is two bytes on the Arduino, 'sizeof' will return twice the number of elements.  Try this:

Code:
int y = sizeof(buffer) / sizeof(buffer[0]);

Remember that the compiler will evaluate the two 'sizeof's and the division.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int i=0, j = BUFFERSIZE-1; i< BUFFERSIZE/2; i++, j--)
  {
    int temp = buffer[i];
    buffer[i] = buffer[j];
    buffer[j] = temp;
  }
Bonus marks for eliminating "temp"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Bonus marks for eliminating "temp"

I'll just leave this here: http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without the int j, but I expect it will execute slower;  pointer math might be faster
Code:
for (int i=0; i< BUFFERSIZE/2; i++)
  {
    int temp = buffer[i];
    buffer[i] = buffer[BUFFERSIZE-1 - i ];
    buffer[BUFFERSIZE-1 - i] = temp;
  }
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Opacity, I agree with.
"Modern CPU architecture" and "AVR" don't fit in the same sentence  ;-)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the AVR chips do have a pipeline (even though that can easily be ignored). But I was actually mostly referring to compiler optimizations.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well, the AVR chips do have a pipeline
PICs do too - I wonder why they're so slow?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: