icse-promo

Question

Write a program to declare a square matrix a[][] of order (m × m) where ‘m’ is the number of rows and the number of columns such that ‘m’ must be greater than 2 and less than 20. Allow the user to input integers into this matrix. Display appropriate error message for an invalid input. Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.

Test your program for the following data and some random data:

 

				
					Example 1:
INPUT: M = 3
4    16   12
8    2    14
6    1    3
OUTPUT:
ORIGINAL MATRIX
4    16   12
8    2    14
6    1    3
MIRROR IMAGE MATRIX
12   16   4
14   2    8
3    1    6
Example 2:
INPUT: M = 22
OUTPUT: SIZE OUT OF RANGE

				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class MirrorMatrix
{
    public static void main(String args[])
    {
        int i=0,j=0,m=0,row=0,col=0;
        int arr[][],mirror[][];
        Scanner sc=new Scanner(System.in);
        System.out.print("M = ");
        m = sc.nextInt();
        if(m < 2 || m > 20)
        {
            System.out.println("SIZE OUT OF RANGE");
            
        }
        else
        {
            arr = new int[m][m];
            System.out.println("Enter matrix elements:");
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < m; j++)
                {
                    arr[i][j] = sc.nextInt();
                }
            }
            System.out.println("ORIGINAL MATRIX");
            
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < m; j++)
                {
                    System.out.print(arr[i][j] + "\t");
                }
                System.out.println();
            }
            mirror= new int[m][m];
            row = 0;
            col = m - 1;
        
            for(i = 0; i < m; i++)
            {
                row = 0;
                for(j = 0; j < m; j++)
                {
                    mirror[row][col] = arr[i][j];
                    col--;
                }
                row++;
            }
            
            System.out.println("MIRROR MATRIX");
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < m; j++)
                {
                    System.out.print(mirror[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *