简体   繁体   中英

Matrix not rotating 90 degree to Right

I want to rotate matrix 90 degree to right, using a single dimension array but it does not give the desired output

I used a simple method before and it did work, so for practice i tried to do it with the help of a single dimension array.

import java.util.Scanner;
public class nine
{
    void main()
    {
        int M, i ,j;
        int z=0;
        int arr[][], x[];

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX");
        M=sc.nextInt();

        arr=new int[M][M];
        x=new int[M*M];
        System.out.println("ENTER THE ELEMENTS IN MATRIX ");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        System.out.println("ORIGINAL MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        
        /* giving matrix x the value of arr from bottom. 
         * example matrix-
            1 2 3 
            4 5 6 
            7 8 9 
            value stored in x- {7 8 9 4 5 6 3 2 1}
        */
        for(i=M-1;i>0;i--)
        {
            for(j=0;j< M;j++)
            {
                x[z]=arr[i][j];
                z++;
            }
        }
        z=0; 
        /* 
         * Adding values of matrix x in matrix arr veritcally. 
         * 
           */
        for(j=0;j< M;j++)
        {
            for(i=0;i< M;i++)
            {
                arr[i][j]= x[z];
                z++;
            }
        }
         /* 
         * Desired output from the example input- 
         *      7 4 1 
                8 5 2 
                9 6 3 
         * 
           */
        System.out.println("ROTATED MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" " );

            }
            System.out.println( );
        }
    }
}


The original array is double dimension and gives value to the single dimension array in the order stated.

The output I want is:

图像1

The output I am getting is is:

img2

To rotate, you need something like this:

int arr[][];
int dest[][];

arr = new int[M][M];
dest = new int[M][M];

for (int i=0; i<M, i++) {
  for (int j=0; j<M, j++) {
    dest[M-j-1][i] = arr[i][j];
  }
}

also, you count down loops need to include zero

for(i=M-1;i>0;i--)

should be

for(i=M-1;i>=0;i--)

( turning the comment by Ricky Mo into an answer with minimal changes and explanation )

You are very close. No need to fundamentally change your approach.
However, the output you get shows clearly that your code does not write to the last column.
An obvious conclusion is that your loop setups are not correct.
And indeed (as Ricky Mo put succinctly):

change for(i=M-1;i>0;i--) to for(i=M-1;i>=0;i--)

Obviously it iterates once more often.
And that ends up in the right (ie the 0 values column) because of the indexing magic, which you correctly implemented.

And the output is:

ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX
ENTER THE ELEMENTS IN MATRIX 

------------------------------------------------------
ORIGINAL MATRIX 

1 2 3 
4 5 6 
7 8 9 

------------------------------------------------------
ROTATED MATRIX 

7 4 1 
8 5 2 
9 6 3 

eg here:
https://www.tutorialspoint.com/compile_java_online.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM