Pages: [1]   Go Down
Author Topic: Shifting rows in a double array  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 517
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an big array of unsigned bytes
uint8_t  myAry[48][60]

Every hour I want to shift everything up one row and add new values to the last row.  Here's an example with a smaller array 3 x 4 array

0)  34, 55, 93, 90
1)  12, 22, 54, 49
2)  87, 21, 88, 96

Now I want row 0 to drop off, row 1 to shift to row 0, row 2 to shift to row 1 and row 2 gets new data.  For example
 
0)  12, 22, 54, 49
1)  87, 21, 88, 96
2)  88, 49, 36, 29

Is there an efficient way to do this with shifting bits or something?  I could write some loops to do it, but I'm hoping there's a better way.




Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

On what board? 48 * 60 = 2880 bytes, which is more than you have on a Uno anyway.

A memmove should do the trick, anyway.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why go through all the trouble of moving things?  Define a couple of pointers and work it like a circular array.
Logged

New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 517
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why go through all the trouble of moving things?  Define a couple of pointers and work it like a circular array.

Could you give me an example of what you mean?  I don't know what a circular array is. 

BTW, I'm using a Mega 2560.
Logged

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

Quote
Could you give me an example of what you mean?
The serial receive buffer is an example.
It has a head and tail pointer.
New characters are added to the tail by the receive interrupt, and taken off the head by the "read" method.
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.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why go through all the trouble of moving things?  Define a couple of pointers and work it like a circular array.

I'd do it with index values rather than pointers.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
I'd do it with index values rather than pointers.

Since C allows you to do stuff like:
Code:
int array [10];
int* pArray = array;

void setup ()
{
  int index = 5;
  array [index] = 17;
  int y = index [array];
  int x = index [pArray];
}
, it can be very hard to tell the difference sometimes!
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.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agreed that there are lots of ways to make it work, but the approach I'd rather use is to access the array using an explicit index value for each dimension, which can then be range-checked and wrapped as necessary. IMO this makes for an implementation which can easily be understood and seen to be correct.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

New Jersey
Offline Offline
God Member
*****
Karma: 2
Posts: 517
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agreed that there are lots of ways to make it work, but the approach I'd rather use is to access the array using an explicit index value for each dimension, which can then be range-checked and wrapped as necessary. IMO this makes for an implementation which can easily be understood and seen to be correct.

I understand the concept of pointers, but I have a hard time using them.  I really have to put on my thinking cap so I don't get confused.  I think I can figure this out using an index, that seems pretty straight forward. 

What do you mean by "wrapped"?
Logged

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

"wrapped" as in "wrap-around", as in don't go below the lowest-addressable item or above the highest.
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: