Go Down

### Topic: Reverse order of Integer array. (Read 5077 times)previous topic - next topic

#### lassemajsan

##### Aug 25, 2011, 09:50 pm
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?

#### Onions

#1
##### Aug 25, 2011, 09:58 pm
Code: [Select]
`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.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!)

#### robtillaart

#2
##### Aug 25, 2011, 10:17 pm
an inplace reverse
Code: [Select]
`#define BUFFERSIZE 123int 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;  }}`
Rob Tillaart

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

#### lassemajsan

#3
##### Aug 25, 2011, 10:20 pm
Big thanks for the fast reply! Great support!

#### retrolefty

#4
##### Aug 25, 2011, 11:04 pm

Code: [Select]
`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); ?

#### dominikh

#5
##### Aug 25, 2011, 11:05 pm

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.

#### anachrocomputer

#6
##### Aug 26, 2011, 12:00 pm

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: [Select]
`int y = sizeof(buffer) / sizeof(buffer[0]);`

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

#### AWOL

#7
##### Aug 26, 2011, 12:56 pm
Code: [Select]
`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"
"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.
I speak for myself, not Arduino.

#### dominikh

#8
##### Aug 26, 2011, 01:28 pm

Bonus marks for eliminating "temp"

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

#### robtillaart

#9
##### Aug 26, 2011, 02:56 pm
Without the int j, but I expect it will execute slower;  pointer math might be faster
Code: [Select]
`for (int i=0; i< BUFFERSIZE/2; i++)  {    int temp = buffer[i];    buffer[i] = buffer[BUFFERSIZE-1 - i ];    buffer[BUFFERSIZE-1 - i] = temp;  }`
Rob Tillaart

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

#### AWOL

#10
##### Aug 26, 2011, 04:31 pm
Quote
I'll just leave this here: http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice

Opacity, I agree with.
"Modern CPU architecture" and "AVR" don't fit in the same sentence  ;-)
"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.
I speak for myself, not Arduino.

#### dominikh

#11
##### Aug 26, 2011, 04:39 pm
Well, the AVR chips do have a pipeline (even though that can easily be ignored). But I was actually mostly referring to compiler optimizations.

#### AWOL

#12
##### Aug 26, 2011, 04:42 pm
Quote
Well, the AVR chips do have a pipeline

PICs do too - I wonder why they're so slow?
"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.
I speak for myself, not Arduino.

Go Up