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 SOLVED 2020 QUESTION 2: Matrix-Octal

Monday, August 24, 2020

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2020 QUESTION 2: Matrix-Octal

 

Question 2:

Write a program to declare a matrix A[][]of order ( M*N) where 'M' is the number of rows

 and 'N' is the number of columns such that the value of "M' must be greater than 0 and 

 less than  10 and the value of 'N' must be greater than 2 and less than 6. Allow the 

user to input digits (0-7) only at each location, such that each row represents an octal 

number.

 

Example:

 

231(decimal equivalent of 1st row=153 i.e. 2*8² +3*8¹ +1*8⁰)   

405(decimal equivalent of 1st row=261 i.e.4*8¹+0*8¹+5*8⁰ )

156(decimal equivalent of 1st row=110 i.e. 1*8²+5*8¹+6*8⁰)

 

Perform the following tasks on the matrix:

 

Display the original matrix.

Calculate the decimal equivalent for each row and display as per the format given below.

 

Example 1:

Input:

ENTER ROW SIZE M=3          

ENTER COLUMN SIZE  N=4           

ENTER ELEMENTS FOR ROW 1: 1 1 3 7          

ENTER ELEMENTS FOR ROW 2: 2 1 0 6           

ENTER ELEMENTS FOR ROW 3: 0 2 4 5

Output:           

FILLED MATRIX   DECIMAL EQUIVALENT                         

1 1 3 7       607                            

2 1 0 6         1094                            

0 2 4 5         165

 

Example 2:

Input:

ENTER ROW SIZE M=3         

ENTER COLUMN SIZE N=3           

ENTER ELEMENTS FOR ROW 1: 0 2 8

Output:

INVALID INPUT

 

Example 3:

Input:

ENTER ROW SIZE  M=3          

ENTER COLUMN SIZE N=9

Output:OUT OF RANGE




import java.util.*;

public class ISC2020Q2 {

       public static void main(String[] args) {

       int M,N;

       System.out.println("Input:");

       Scanner in = new Scanner(System.in);

       do {

             System.out.println("ENTER ROW SIZE:");

             M = in.nextInt();

             System.out.println("ENTER COLUMN SIZE:");

             N = in.nextInt();

             if(M<0 || M>9 || N <3 || N > 5)

                    System.out.println("OUT OF RANGE, TRY AGAIN:");

             }while(M<0 || M>9 || N <3 || N > 5);

       Matrix obj = new Matrix(M,N);

       obj.inputElements();

       System.out.println("Filled Matrix       Decimal Equivalent");

       obj.display();

       in.close();

 

      }

}

class Matrix {

       private void convertsRowToOctalEquivalent() {

              int temp;

              for(int i=0;i<M;i++) {

                     rowOctalEquivalent[i]=0;

                     temp=N-1;

            for(int j=0;j<N;j++) {

              rowOctalEquivalent[i] += mat[i][j]*(int)Math.pow(8,temp);

              temp--;

              }

            }

             

       }

   

      public void display() {

            for(int p=0;p<M;p++)

                   for(int q=0;q<N;q++)

                         if(MAX_MATRIX < mat[p][q])

                                MAX_MATRIX = mat[p][q];

            String s,element;

            s=Integer.toString(MAX_MATRIX);

            for(int i=0;i<M;i++) {

                   for(int j=0;j<N;j++) {

                          element=Integer.toString(mat[i][j]); // for formatted output

                         int tmp=element.length();

                         while(tmp !=s.length()) {

                                       element +=" ";

                                       tmp++;

                                       }

                         System.out.print(element+"  ");

                         }

                 System.out.print("       "+rowOctalEquivalent[i]); 

                 System.out.println();

            }

           

      }

      public void inputElements() {

             Scanner in = new Scanner(System.in);

            System.out.println("Enter the values of the matrix:");

            for(int i=0;i<M;i++) {

                     System.out.println("Enter the elements of row :"+(i+1));

                   for(int j=0;j<N;j++) {

                         mat[i][j]=in.nextInt();

                         if(mat[i][j]<0 || mat[i][j] > 7) {

                                System.out.println("Invalid input,try again:");

                                j--;   // Or you can simply break, as asked in the question

                          }

                   }

            }

            in.close();

            convertsRowToOctalEquivalent(); // Calculating the octal equivalent for each row

      }

      Matrix(int M, int N){

            this.M = M;

            this.N = N;

            mat = new int[M][N];

            rowOctalEquivalent = new int[M];

            MAX_MATRIX =0;

             }

       private int[][] mat;

       private int M,N;

       private int MAX_MATRIX;

       private int[] rowOctalEquivalent;

}

No comments:

Post a Comment