Thursday, February 27, 2020

2-D Numeric Array Octal To Decimal Conversion


Write  a program to declare a matrixA[][] of order (MxN) 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:

 2 3 1 (decimal equivalent of lst row = 153 i.e. 2x8^2 + 3x8^1 + 1x8^0)

 4 0 5 (decimal equivalent of 2nd row=261 i.e. 4x8^2 + 0x8^1 + 5x.8^0)

 1 5 6 (decimal equivalent of 3rd row=110 i.e. lx.8^2 + 5x.8^1 + 6x8^0)


Perform the following tasks on the matrix:

(a) Display the original matrix.

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



Example 1

 INPUT: M=I
               N=3

ENTER ELEMENT SFOR ROW 1 : 1 4 4

OUTPUT: FILLED MATRIX      DECIMAL EQUIVALENT

                   1 4 4                           100


import java.io.*;
class OctalDeci
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  int arr[][];
  void show() throws Exception
 {
      int m,n,i,j,f=0;
      System.out.print("\nEnter value of 'm': ");
      m=Integer.parseInt(br.readLine());
      if(m<=0 || m>=10)
      {
           System.out.println("\nOUT OF RANGE");
           return;
      }
      System.out.print("\nEnter value of 'n': ");
      n=Integer.parseInt(br.readLine());
      if(n<=2 || n>=6)
      {
           System.out.println("\nOUT OF RANGE");
           return;
      }
      arr=new int[m][n];
       for(i=0;i<=m-1;i++)
      { 
        System.out.println("\nENTER ELEMENTS FOR ROW "+(i+1) + ":");
          for(j=0;j<=n-1;j++)
          {         
           arr[i][j]=Integer.parseInt(br.readLine());
           if(arr[i][j]<0 arr="" i="" j="">7)
           break;
        }
        if(j<=n-1)
        break;
      }
      if(i<=m-1)

      {
          System.out.print("\nINVALID INPUT");
          return;
        }
        System.out.println("\nFILLED MATRIX       DECIMAL EQUIVALENT");
        for(i=0;i<=m-1;i++)
        {
            f=0;
            for(j=0;j<=n-1;j++)
            {
                System.out.print(arr[i][j]+ " ");
                f=f+arr[i][j]*(int)Math.pow(8,n-(j+1));
            }
                System.out.print("          "+f);
                System.out.println();
            }
      }
       public static void main(String args[]) throws Exception
     {
         OctalDeci ob=new OctalDeci();
         ob.show();
      }
    }


Variable Description


Type
Variable
Purpose
Scope
BufferedReader
br
Intake values from user
Used within void show () function
int
m
Stores the first dimensional size of 2 D Array ‘arr’
Used within void show () function
int
n
Stores the first dimensional size of 2 D Array ‘arr’
Used within void show () function
Int
arr[][]
2 D Array to store octal values
Used within void show () function
int
i
Loop control variable
Used within void show () function
int
j
Loop control variable
Used within void show () function


Algorithm

Step 1: Create BufferedReader class object br
Step 2: Create int type variables m,n,i,j and f with 0 as initial value at f
Step 3: Take number of rows and columns of the 2 D Array from user and store in m and n
Step 4: Take the Octal values from user using a nested loop and store in the array ‘arr[][]’.
Step 5: Take proper precaution so that all the values entered are octal values.
Step 6: Access row wise values from the array using nested loop and generate decimal equivalent of the octal values and display.

Step 7: End

BACK TO 2020 ISC COMPUTER PRACTICAL PAPER: CLICK HERE
     

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner