Thread: How Do you rotate a multidimensional array?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    Question How Do you rotate a multidimensional array?

    lets say i have an array like this.....

    array[4][4]

    and this is the data in the array

    array[0] { 1,1,1,1}
    array[1] { 2,2,2,2}
    array[2] { 3,3,3,3}
    array[3] { 4,4,4,4}

    how do i rotate the array to the left so it is converted to...

    array[0] { 1,2,3,4}
    array[1] { 1,2,3,4}
    array[2] { 1,2,3,4}
    array[3] { 1,2,3,4}

    I am sure it must be a basic problem but i cannnot think of it!!!

    Could someone help me please??
    werdy666!

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Can we assume you have a square matrix(array) to work with or do the columns and rows vary?


    Mr. C.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    33
    The array is square.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do i rotate the array to the left
    Simple enough, just think differently when you traverse the array:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i, j;
        int a[4][4] = 
        {
            { 1,1,1,1 },
            { 2,2,2,2 },
            { 3,3,3,3 },
            { 4,4,4,4 },
        };
    
        cout<<"Original array: "<<endl;
        for ( i = 0; i < 4; ++i ) {
            for ( j = 0; j < 4; ++j )
                cout<< a[i][j] <<' ';
            cout.put ( '\n' );
        }
    
        cout<<"\nRotated array: "<<endl;
        for ( i = 0; i < 4; ++i ) {
            for ( j = 0; j < 4; ++j )
                cout<< a[j][i] <<' ';
            cout.put ( '\n' );
        }
        return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  2. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  3. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM