Go Down

Topic: Shifting rows in a double array (Read 616 times) previous topic - next topic

ScottG

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.





Nick Gammon

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

A memmove should do the trick, anyway.

Delta_G

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

ScottG


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.

AWOL

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.
"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.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

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


Since C allows you to do stuff like:
Code: [Select]
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!
"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.

PeterH

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 only provide help via the forum - please do not contact me for private consultancy.

ScottG


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"?

AWOL

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

Go Up