Rotating an array left by one element

Discussion in 'C++' started by ravxm, Jan 27, 2007.

  1. ravxm

    ravxm Guest

    I'm trying to rotate an array of ints by one element to the left. I'm a
    newbie on this so please have a little patience...
    I want to rotate the array so if I have 12345, after one rotation I get
    23451 and after another I get 34512.
    I'm getting the following output after three rotations:
    12345

    23452
    34523
    45234

    Here's my initial code:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    const int MAX_SIZE=5;
    int MyArray[MAX_SIZE];
    void shiftLeft(int tmparr[MAX_SIZE]);
    void PrintArray(int tmparr[MAX_SIZE]);
    int main(int argc,char *argv[])
    {
    for(int i=0;i<MAX_SIZE;i++)
    {
    MyArray=i+1;
    }
    PrintArray(MyArray);
    shiftLeft(MyArray);
    PrintArray(MyArray);
    shiftLeft(MyArray);
    PrintArray(MyArray);
    shiftLeft(MyArray);
    PrintArray(MyArray);
    system("Pause");
    }

    void shiftLeft(int tmparr[MAX_SIZE])
    {
    for(int i=0;i<MAX_SIZE;i++)
    {
    MyArray=tmparr[(i+1)%MAX_SIZE];

    }
    }

    void PrintArray(int tmparr[MAX_SIZE])
    {
    for(int i=0;i<MAX_SIZE;i++)
    {
    cout<<"Array position: "<<i<<endl;
    cout<<"Contents: "<<tmparr<<endl;

    }
    cout<<"\n\n
    ********************************************************"<<endl;
    }
    ravxm, Jan 27, 2007
    #1
    1. Advertising

  2. ravxm

    Jack Klein Guest

    On 26 Jan 2007 22:39:34 -0800, "ravxm" <> wrote in
    comp.lang.c++:

    > I'm trying to rotate an array of ints by one element to the left. I'm a
    > newbie on this so please have a little patience...
    > I want to rotate the array so if I have 12345, after one rotation I get
    > 23451 and after another I get 34512.
    > I'm getting the following output after three rotations:
    > 12345
    >
    > 23452
    > 34523
    > 45234
    >
    > Here's my initial code:
    >
    > #include <iostream>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > const int MAX_SIZE=5;
    > int MyArray[MAX_SIZE];
    > void shiftLeft(int tmparr[MAX_SIZE]);
    > void PrintArray(int tmparr[MAX_SIZE]);
    > int main(int argc,char *argv[])
    > {
    > for(int i=0;i<MAX_SIZE;i++)
    > {
    > MyArray=i+1;
    > }
    > PrintArray(MyArray);
    > shiftLeft(MyArray);
    > PrintArray(MyArray);
    > shiftLeft(MyArray);
    > PrintArray(MyArray);
    > shiftLeft(MyArray);
    > PrintArray(MyArray);
    > system("Pause");
    > }
    >
    > void shiftLeft(int tmparr[MAX_SIZE])
    > {
    > for(int i=0;i<MAX_SIZE;i++)
    > {
    > MyArray=tmparr[(i+1)%MAX_SIZE];
    >
    > }
    > }


    The code in your shiftLeft function would work the way you want it to
    if you were creating a rotated copy of the original in a different
    array. But, as you can see, it won't work when the source and
    destination arrays are the same.

    Just step through it like this to see:

    Before the first iteration:

    MyArray is 12345, i is 0, i + 1 is 1

    So your code is effectively this for the very first iteration:

    MyArray[0]=tmparr[1];

    Since the memory pointed to by tmparr is MyArray, you write 2 over the
    1, and the 1 is gone forever.

    If you want to rotate an array in place, you need a temporary variable
    to store each value before you overwrite it.

    > void PrintArray(int tmparr[MAX_SIZE])
    > {
    > for(int i=0;i<MAX_SIZE;i++)
    > {
    > cout<<"Array position: "<<i<<endl;
    > cout<<"Contents: "<<tmparr<<endl;
    >
    > }
    > cout<<"\n\n
    > ********************************************************"<<endl;
    > }


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 27, 2007
    #2
    1. Advertising

  3. ravxm

    ravxm Guest

    On Jan 27, 2:58 am, Jack Klein <> wrote:
    > On 26 Jan 2007 22:39:34 -0800, "ravxm" <> wrote in
    > comp.lang.c++:
    >
    >
    >
    > > I'm trying to rotate an array of ints by one element to the left. I'm a
    > > newbie on this so please have a little patience...
    > > I want to rotate the array so if I have 12345, after one rotation I get
    > > 23451 and after another I get 34512.
    > > I'm getting the following output after three rotations:
    > > 12345

    >
    > > 23452
    > > 34523
    > > 45234

    >
    > > Here's my initial code:

    >
    > > #include <iostream>
    > > #include <cstdlib>

    >
    > > using namespace std;

    >
    > > const int MAX_SIZE=5;
    > > int MyArray[MAX_SIZE];
    > > void shiftLeft(int tmparr[MAX_SIZE]);
    > > void PrintArray(int tmparr[MAX_SIZE]);
    > > int main(int argc,char *argv[])
    > > {
    > > for(int i=0;i<MAX_SIZE;i++)
    > > {
    > > MyArray=i+1;
    > > }
    > > PrintArray(MyArray);
    > > shiftLeft(MyArray);
    > > PrintArray(MyArray);
    > > shiftLeft(MyArray);
    > > PrintArray(MyArray);
    > > shiftLeft(MyArray);
    > > PrintArray(MyArray);
    > > system("Pause");
    > > }

    >
    > > void shiftLeft(int tmparr[MAX_SIZE])
    > > {
    > > for(int i=0;i<MAX_SIZE;i++)
    > > {
    > > MyArray=tmparr[(i+1)%MAX_SIZE];

    >
    > > }
    > > }The code in your shiftLeft function would work the way you want it to

    > if you were creating a rotated copy of the original in a different
    > array. But, as you can see, it won't work when the source and
    > destination arrays are the same.
    >
    > Just step through it like this to see:
    >
    > Before the first iteration:
    >
    > MyArray is 12345, i is 0, i + 1 is 1
    >
    > So your code is effectively this for the very first iteration:
    >
    > MyArray[0]=tmparr[1];
    >
    > Since the memory pointed to by tmparr is MyArray, you write 2 over the
    > 1, and the 1 is gone forever.
    >
    > If you want to rotate an array in place, you need a temporary variable
    > to store each value before you overwrite it.
    >
    > > void PrintArray(int tmparr[MAX_SIZE])
    > > {
    > > for(int i=0;i<MAX_SIZE;i++)
    > > {
    > > cout<<"Array position: "<<i<<endl;
    > > cout<<"Contents: "<<tmparr<<endl;

    >
    > > }
    > > cout<<"\n\n
    > > ********************************************************"<<endl;
    > > }--

    > Jack Klein
    > Home:http://JK-Technology.Com
    > FAQs for
    > comp.lang.chttp://c-faq.com/
    > comp.lang.c++http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html




    That was a very nice explanation, I'll try it out, thanks
    ravxm, Jan 27, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,107
    news.frontiernet.net
    Apr 16, 2004
  2. Roozbeh GHolizadeh

    rotating an array....

    Roozbeh GHolizadeh, May 4, 2004, in forum: C Programming
    Replies:
    4
    Views:
    600
    Roozbeh GHolizadeh
    May 5, 2004
  3. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help on table align on left of page vs left hanging indent

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Jul 10, 2007, in forum: XML
    Replies:
    2
    Views:
    988
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Jul 16, 2007
  4. Vik Rubenfeld
    Replies:
    20
    Views:
    1,124
    Neredbojias
    Jan 18, 2008
  5. pc
    Replies:
    2
    Views:
    1,294
    crisgoogle
    Jun 8, 2011
Loading...

Share This Page