Thread: what is wrong? how can i remake this code to rotate clockwaise

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    what is wrong? how can i remake this code to rotate clockwaise

    Code:
     
    
            static void Main(string[] args)
            {
                int[,] matr =  { { 1,2 },
                                 { 3,4 },
                                 { 5,6 },
                                 { 7,8 }
    
    
                               };
    
    
    
    
    
    
                for (int i = 0; i < matr.GetLength(0); i++)
                {
    
    
                    for (int j = 0; j < matr.GetLength(1); j++)
                    {
                        Console.Write(matr[i, j] + "");
                    }
                    Console.WriteLine();
                }
    
    
                Console.WriteLine("after rotated");
                int[,] rotated = new int[2, 4];
                rotated = Rotate(matr);
                for (int m = 0; m < rotated.GetLength(0); m++)
                {
                    for (int n = 0; n < rotated.GetLength(1); n++)
                    {
                        Console.Write(rotated[m, n] + "");
                    }
                    Console.WriteLine();
                }
    
    
    
    
            }
    
    
            public static int[,] Rotate(int[,] ExArray)
            {
                int[,] newarray = new int[ExArray.GetLength(1), ExArray.GetLength(0)];
                int row = 0;
                int column = 0;
                for (int j = ExArray.GetLength(1) - 1; j >= 0; j--)
                {
                    for (int i = 0; i < ExArray.GetLength(0); i++)
                    {
                        newarray[row, column] = ExArray[i, j];
                        column++;
                    }
                    row++;
                    column = 0;
                }
                return newarray;
            }
    
    
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think it's a matter of going through the inner loop in reverse, as well.
    You figure out that the indexes should pair this way:

    row=0, column=1; i=3, j=1
    row=0, column=2; i=2, j=1
    row=0, column=3; i=1, j=1
    row=0, column=4; i=0, j=1
    row=1, column=1; i=3, j=0
    row=1, column=2; i=2, j=0
    row=1, column=3; i=1, j=0
    row=1, column=4; i=0, j=0

    Now it looks like the array turned to the right by 90 degrees.
    Last edited by whiteflags; 09-09-2016 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2016, 07:24 PM
  2. Ping Pong remake
    By Akkernight in forum Game Programming
    Replies: 3
    Last Post: 02-25-2009, 07:56 AM
  3. What's wrong with STL rotate algorithm?
    By Mathsniper in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2006, 08:54 AM
  4. Remake of nintendo Archon.
    By seventwenty99 in forum Game Programming
    Replies: 5
    Last Post: 06-02-2002, 05:39 PM
  5. Spectrum Game PC Remake
    By Robert_Ingleby in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 06:26 AM

Tags for this Thread