Computer Science
Write a program to declare a matrix A[][] of order (M x 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:
2 | 3 | 1 | (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80) |
4 | 0 | 5 | (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80) |
1 | 5 | 6 | (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80) |
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.
Test your program for the following data and some random data:
Example 1:
INPUT:
M = 1
N = 3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
FILLED MATRIX | DECIMAL EQUIVALENT | ||
---|---|---|---|
1 | 4 | 4 | 100 |
Example 2:
INPUT:
M = 3
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 3:
INPUT:
M = 3
N = 3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M = 4
N = 6
OUTPUT:
OUT OF RANGE
Java
Java Arrays
ICSE Prac 2020
15 Likes
Answer
import java.util.Scanner;
public class OctalMatrix
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = in.nextInt();
if (m <= 0 || m >= 10 || n <= 2 || n >= 6) {
System.out.println("OUT OF RANGE");
return;
}
int a[][] = new int[m][n];
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;
}
}
}
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
for (int i = 0; i < m; i++) {
int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n - j - 1 );
System.out.print(a[i][j] + " ");
}
System.out.print("\t\t" + decNum);
System.out.println();
}
}
}
Output
![BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-1.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-2.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-3.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x 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: 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. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 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: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-4.jpg)
Answered By
6 Likes
Related Questions
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found" using Binary search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.
Consider the following two-dimensional array and answer the questions given below:
int x[ ][ ] = {{4,3,2}, {7,8,2}, {8, 3,10}, {1, 2, 9}};
(a) What is the order of the array?
(b) What is the value of x[0][0]+x[2][2]?
Which of the following is a valid way to declare and initialize an integer array to store the ages of 50 students?
Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033