Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 2

Monday, March 11, 2019

ISC COMPUTER SCIENCE PRACTICAL 2015 QUESTION 2

/*Write a program to declare a square matrix A[][] of order M x M where ‘M’ is the
number of rows and the number of columns, such that M must be greater than 2 and less
than 10. Accept the value of M as user input. Display an appropriate message for an
invalid input. Allow the user to input integers into the matrix. Perform the following
tasks:
a) Display the original matrix.
b) Rotate the matrix 90ยบ clockwise as shown below:
Original matrix Rotated matrix
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3
c) Find the sum of the elements of the four corners of the matrix. Test your program
for the following data and some random data:
Example 1:
INPUT : M=3
 3 4 9
 2 5 8
 1 6 7
OUTPUT:
ORIGINAL MATRIX
3 4 9
2 5 8
 1 6 7
MATRIX AFTER ROTATION
 1 2 3
 6 5 4
 7 8 9
Sum of the corner elments = 20
Example 2:
INPUT : M=4
 1 2 4 9
 2 5 8 3
 1 6 7 4
 3 7 6 5
OUTPUT:
ORIGINAL MATRIX
 1 2 4 9
 2 5 8 3
 1 6 7 4
 3 7 6 5
MATRIX AFTER ROTATION
 3 1 2 1
 7 6 5 2
 6 7 8 4
 5 4 3 9
Sum of the corner eements= 18
EXAMPLE 3.
INPUT : M = 14
OUTPUT:
 SIZE OUT OF RANGE*/


import java.util.*;
public class ISC2015Q2 {
       public static void main(String[] args) {
             int M;
             Scanner in = new Scanner(System.in);
             do {
                    System.out.println("Enter the order of the matrix");
                    M = in.nextInt();
                    if(M < 2 || M > 10)
            System.out.println("INVALID INPUT, TRY AGAIN:");
             }while(M < 2 || M > 10);
             MatrixRotation Mat_Object = new MatrixRotation(M);
             Mat_Object.MatrixInput();
             Mat_Object.DisplayInputMatrix();
             Mat_Object.RotateMatrix();
             Mat_Object.DisplayRotatedMatrix();
             System.out.println("Sum of corner elements:"+Mat_Object.SumOfCornerElements());
             }
}

class MatrixRotation {

       MatrixRotation(int M) {
             order = M;
             Sum_of_Corner_Elements = 0;
             Mat = new int[order][order];
             Rotated_Mat = new int[order][order];
       }

       public int SumOfCornerElements() {
             return (Rotated_Mat[0][0]+Rotated_Mat[0][order-1]+Rotated_Mat[order-1][0]+Rotated_Mat[order-1][order-1]);
       }

       public void DisplayRotatedMatrix() {
             System.out.println("OUTPUT");
             System.out.println("Rotated matrix:");
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          System.out.print(Rotated_Mat[i][j]+" ");
                    System.out.println();
             }
       }

       public void RotateMatrix(){
             int k;
             k=order;
             for(int i=0; i<order;i++) {
                    for(int j=0;j<order;j++)
                          Rotated_Mat[j][k-1]=Mat[i][j];
                    k--;
             }
       }
       public void MatrixInput() {
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the elements of the matrix(row-wise)"+order+"X"+order);
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          Mat[i][j]=in.nextInt();
             }
       }
      
       public void DisplayInputMatrix() {
             System.out.println("INPUT MATRIX:");
             for(int i=0;i<order;i++) {
                    for(int j=0;j<order;j++)
                          System.out.print(Mat[i][j]+" ");
                    System.out.println();
             }
       }
       private int order;
       private int[][] Mat;
       private int[][] Rotated_Mat;
       private int Sum_of_Corner_Elements;
       }

No comments:

Post a Comment