Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: August 25, 2011, 02:50:05 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? Thanks in advance.
|
|
|
|
|
Logged
|
|
|
|
|
Newcastle, England
Offline
Sr. Member
Karma: 2
Posts: 489
Always learning!
|
 |
« Reply #1 on: August 25, 2011, 02:58:40 pm » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: August 25, 2011, 03:17:22 pm » |
an inplace reverse #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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #3 on: August 25, 2011, 03:20:56 pm » |
Big thanks for the fast reply! Great support!
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #4 on: August 25, 2011, 04:04:02 pm » |
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
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #5 on: August 25, 2011, 04:05:52 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.
|
|
|
|
|
Logged
|
|
|
|
|
Bristol, UK
Offline
Edison Member
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
|
 |
« Reply #6 on: August 26, 2011, 05:00:25 am » |
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: 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
Brattain Member
Karma: 137
Posts: 19024
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: August 26, 2011, 05:56:57 am » |
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.
|
|
|
|
Germany
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #8 on: August 26, 2011, 06:28:01 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #9 on: August 26, 2011, 07:56:13 am » |
Without the int j, but I expect it will execute slower; pointer math might be faster for (int i=0; i< BUFFERSIZE/2; i++) { int temp = buffer[i]; buffer[i] = buffer[BUFFERSIZE-1 - i ]; buffer[BUFFERSIZE-1 - i] = temp; }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19024
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: August 26, 2011, 09:31:56 am » |
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.
|
|
|
|
Germany
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #11 on: August 26, 2011, 09:39:03 am » |
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
Brattain Member
Karma: 137
Posts: 19024
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: August 26, 2011, 09:42:20 am » |
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.
|
|
|
|
|