Re: circular shift array
- From: Richard <rgrdev@xxxxxxxxx>
- Date: Tue, 31 Jul 2007 11:07:45 +0200
dan <dagoldman@xxxxxxxxx> writes:
On Jul 30, 7:36 pm, Barry Schwarz <schwa...@xxxxxxxxx> wrote:
On Mon, 30 Jul 2007 22:04:42 -0500, "Bint" <b...@xxxxxxxx> wrote:
Hi,
What is a simple way to shift the elements in an array, circularly? Is
there a way to do it so that you don't need a separate storage array?
IE I want to shift array A{1,2,3,4,5,6,7,8} by 3 to the right. The result
would be B{6,7,8,1,2,3,4,5)
You don't need a separate array; one temporary object to hold the
rightmost value is sufficient.
Save rightmost value
Loop through array from right to left starting at next to
rightmost value
Save value in next element to the right
Store saved value in first element
Repeat as often as necessary.
You could make a function out of it (or even a macro)
void rshift_int(int array[], size_t num_elements, int
iterations){...}
Remove del for email
Barry is correct. I did this kind of thing.
One suggestion. If you have a bunch of arrays, instead of a temporary
object, you may want to make each array one longer than needed, and
use the extra element as the temporary object. It may make your code
easier to read and maintain.
Daniel Goldman
or just have the array double its normal size with duplicate copies and
merely shuffle a pointer to the first element .... no Memory move at all
then.
.
- References:
- circular shift array
- From: Bint
- Re: circular shift array
- From: Barry Schwarz
- Re: circular shift array
- From: dan
- circular shift array
- Prev by Date: Re: Unused arguments
- Next by Date: Re: Calling a function with a literal list of strings?
- Previous by thread: Re: circular shift array
- Next by thread: Re: circular shift array
- Index(es):
Relevant Pages
|