how do i rotate an array in C++?????
Results 1 to 3 of 3

Thread: how do i rotate an array in C++?????

  1. #1
    Member
    Join Date
    Mar 2003
    Posts
    38

    Question how do i rotate an array in C++?????

    can somebody tell me how to rotate a c++ array x times to the left or x times to the right?????
    please i really need to know

    thanks

  2. #2
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    What exactly do you mean by "rotate". Please be more specific.
    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  3. #3
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007
    Sounds like he means in the graphical sense.

    The easiest way to code it would probably be to loop through the array, and turn all of the values into an ordered list... Then read the list back into the array with different loop conditions... Just some psuedocode stuff here...
    Code:
    //array is called Array[Row][Column]...
    
    list.clearList();
    // read it
    for(int i=0;i<Row;i++)
    {
            for(int j=0;j<Column;j++)
            {
                    list.add(Array[i][j];
            }
    }
    
    //Write it -- Rotation to the right
    for(int i=(Columns-1);i>0;i--)
    {
            for(int j=0;j<Rows;j++)
            {
                    Array[j][i] = list.getNext();
            }
    }
    There are probably some off-by-one errors in there, but I'm lazy.
    [HvC]Terr: L33T Technical Proficiency

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •