A matrix is called Doubly Markov if it satisfies the following conditions:
All elements are greater than or equal to 0.
The sum of each row is equal to 1.
The sum of each column is equal to 1.
The program should
Accept
Nfrom the user, where3 <= N <= 9.Display an appropriate error message if
Nis not in the given range.Allow the user to enter the elements of the matrix and display an error if any entered number is negative.
Check and display whether the created matrix is a Doubly Markov matrix or not.
Test the program with the following data and some random data:
Example 1: INPUT: N = 3 Enter elements in the matrix: 0.5, 0.25, 0.25, 0.25, 0.75, 0.0, 0.25, 0.0, 0.75
OUTPUT: FORMED MATRIX 0.5 0.25 0.25 0.25 0.75 0.0 0.25 0.0 0.75 IT IS A DOUBLY MARKOV MATRIX
Example 2: INPUT: N = 3 Enter elements in the matrix: 1.5, 3, 0.15, 0.25, 4, 1.0, 0.25, 1.0, 3
OUTPUT: FORMED MATRIX 1.5 3 0.15 0.25 4 1.0 0.25 1.0 3 IT IS NOT A DOUBLY MARKOV MATRIX
Example 3: INPUT: N = 3 Enter elements in the matrix: 0.8, -4.0, 0.9, 3.5, 0.9, 3.5, 1.2, 3.2, 3.5
OUTPUT: NEGATIVE NUMBER ENTERED. INVALID ENTRY
Example 4: INPUT: N = 12
OUTPUT: SIZE IS OUT OF RANGE. INVALID ENTRY
Program:
import java.util.Scanner;
public class DoublyMarkav {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the size of the Matrix (between 3 and 9):”);
int n = sc.nextInt();
if (n < 3 || n > 9) {
System.out.println(“SIZE IS OUT OF RANGE. INVALID ENTRY”);
System.exit(0);
}
double[][] a = new double[n][n];
System.out.println(“Enter the Numbers (all must be non-negative):”);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextDouble();
if (a[i][j] < 0) {
System.out.println(“NEGATIVE NUMBER ENTERED. INVALID ENTRY”);
System.exit(0);
}
}
}
System.out.println(“FORMED MATRIX:”);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + “\t”);
}
System.out.println();
}
boolean flag = true;
for (int i = 0; i < n; i++) {
double rowSum = 0, colSum = 0;
for (int j = 0; j < n; j++) {
rowSum += a[i][j];
colSum += a[j][i];
}
if (rowSum != 1.0 || colSum != 1.0) {
flag = false;
break;
}
}
if (flag)
System.out.println(“THE GIVEN MATRIX IS DOUBLY MARKOV”);
else
System.out.println(“THE GIVEN MATRIX IS NOT DOUBLY MARKOV”);
}
}
How this code works?
Here’s a step-by-step explanation of how the provided Java code for checking a Doubly Markov matrix works:
Input Matrix Size
The program prompts the user to enter the size N of the matrix.
It immediately checks if N is within the valid range (3 to 9). If not, it prints an error and stops.
Matrix Data Entry
The user is asked to enter N×N real numbers (doubles), filling the matrix row by row.
For every element, it checks if the entry is negative. If so, the program prints an error and exits.
Display Formed Matrix
After all input, the program prints the matrix in a formatted, tabular way for clarity.
Core Logic – Doubly Markov Check
The program uses a boolean flag (initially true) to track if the given matrix fulfills the properties:
Every element is already confirmed non-negative during entry.
For each row and each column:
Calculates the sum of all row elements (
rowSum) and column elements (colSum).For every i from 0 to N−1, sums up the ith row and ith column.
If either sum is not exactly 1.0 (using equality check), sets the flag to false and breaks.
If all rows and all columns sum to 1, the matrix is classified as Doubly Markov.
Result Output
If the flag remains true after the check, it prints that the matrix is a Doubly Markov matrix.
Otherwise, it states the matrix is not a Doubly Markov matrix.
In Summary:
Validation: Ensures only allowed values and correct matrix size.
Processing: Sums rows and columns to see if every one adds up to 1.
Output: Reports whether the input matrix qualifies as a Doubly Markov matrix.
This workflow ensures that all three required conditions are checked in a user-friendly and robust manner.