Page 341 - Computer science 868 Class 12
P. 341

Ans.  Column Major Wise = B + W( (I – I0) + (J – J0) × R)
                        B = 1025
                        W = 4
                        I = 4, J = 8
                        I0 = –6, J0 = 4
                        R = 10 + 6 + 1 = 17
                        A = 1025 + 4 × ((4 + 6) + (8 – 4) × 17)
                        = 1025 + 4 × (10 + 68)
                        = 1025 + 312
                        = 1337
                    2. A class Trans is defined to find the transpose of a square matrix. A transpose of a matrix is obtained by interchanging the
                      elements of the rows and columns. Example: If size of the matrix = 3, then                 [ISC 2023]
                                                           ORIGINAL         TRANSPOSE
                                                         11   5    7       11   8    1
                                                          8   13   9        5   13   6
                                                          1   6   20        7   9   20
                       Some of the members of the class are given below:
                       Class name                                   :   Trans
                       Data members/instance variable:
                       arr[][]                                      :   to store integers in the matrix
                       m                                            :   integer to store the size of the matrix
                       Methods/Member functions:
                       Trans(int mm)                                :    parameterised constructor to initialise the data member m = mm
                       void fillarray()                             :   to enter integer elements in the matrix
                       void transpose()                             :   to create the transpose of the given matrix
                       void display()                               :    displays the original matrix and the transport matrix by invoking
                                                                      the method transpose()
                       Specify the class Trans giving details of the constructor(), void fillarray(), void transpose() and void display(). Define a main()
                      function to create an object and call the functions accordingly to enable the task.

                   Ans.  import java.util.Scanner;
                       class Trans
                       {
                       int arr[][];
                       int m;
                       public Trans(int mm)
                       {
                       m=mm;
                       arr=new int[m][m];
                       }
                            public void fillarray()
                       {
                       Scanner sc=new Scanner(System.in);
                       System.out.println("Enter the array elements");
                       for(int i=0;i<m;i++)
                       {
                       for(int j=0;j<m;j++)
                       {
                       arr[i][j]=sc.nextInt();
                       }
                       }
                       }
                       public void transpose()
                       {
                       System.out.println("TRANSPOSE");
                       for(int i=0;i<m;i++)
                       {
                       for(int j=0;j<m;j++)


                                                                                                                       339
                                                                                                              Arrays   339
   336   337   338   339   340   341   342   343   344   345   346