I created a program to calculate Markov chain and I have some issue anyone can help me why after the application prints out asking the user for value for the row and column till row 3 it stops and it didn’t ask the user for value P. It straight print “Markov Chain Invalid Row not Equal to 1” and end. Anyone can help me to fix this, please. Thank u in advance.
public class assgmatrix { static void printArray(double arrayfreq[]) { for(double i:arrayfreq) { System.out.println(i); } } static void printArrayM(double arrayfreq[][]) { System.out.println(); System.out.println(Arrays.deepToString(arrayfreq).replace("]","n").replace(",","t|").replaceAll("[\[\]]"," ")); System.out.println(); } static boolean checkMark(double m[][]) { for(int i=0; i<m.length; i++) { double sum=0; for(int j=0; j<m[i].length; j++) sum+=m[i][j]; if(sum !=1) return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Matrix 3 x 3 "); System.out.println(); System.out.println(" Enter The Value for Row 1 Colum 1 :"); double oneM=input.nextDouble(); System.out.println(" Enter The Value for Row 1 Colum 2 :"); double twoM=input.nextDouble(); System.out.println(" Enter The Value for Row 1 Colum 3 :"); double threeM=input.nextDouble(); System.out.println(); System.out.println(" Enter The Value for Row 2 Colum 1 :"); double fourM=input.nextDouble(); System.out.println(" Enter The Value for Row 2 Colum 2 :"); double fiveM=input.nextDouble(); System.out.println(" Enter The Value for Row 2 Colum 3 :"); double sixM=input.nextDouble(); System.out.println(); System.out.println(" Enter The Value for Row 3 Colum 1 :"); double sevenM=input.nextDouble(); System.out.println(" Enter The Value for Row 3 Colum 2 :"); double eightM=input.nextDouble(); System.out.println(" Enter The Value for Row 3 Colum 3 :"); double nineM=input.nextDouble(); System.out.println(); double m[][]= { { oneM, twoM, threeM, fourM, fiveM, sixM, sevenM, eightM, nineM} }; double firstinput=0.0; double secondinput=0.0; double thirdinput=0.0; double checkSum=0.0; if(checkMark(m)) { System.out.println(" Valid Markox Matrix as Sum for each Row is Equal to 1 "); System.out.println(" Enter First P Value in % :"); firstinput=input.nextDouble(); firstinput=firstinput/100; System.out.println(" Enter Second P Value in % :"); secondinput=input.nextDouble(); secondinput=secondinput/100; System.out.println(" Enter Third P Value in % :"); thirdinput=input.nextDouble(); thirdinput=thirdinput/100; checkSum=firstinput+secondinput+thirdinput; if(checkSum==1) { System.out.println("Transition Of Matrix Is : "); printArrayM(m); System.out.println("initial Probibility Vector that had been Entered is :"); System.out.println(" "+firstinput+","+secondinput+","+thirdinput); System.out.println(); System.out.println(" "); double firstRow=(firstinput*m[0][0])+(secondinput*m[1][0])+(thirdinput*m[2][0]); double secondRow=(firstinput*m[0][1])+(secondinput*m[1][1])+(thirdinput*m[2][1]); double thirdRow=(firstinput*m[0][2])+(secondinput*m[1][2])+(thirdinput*m[2][2]); double sum = firstRow+secondRow+thirdRow; String s1=String.format("%.3f", firstRow); String s2=String.format("%.3f", secondRow); String s3=String.format("%.3f", thirdRow); System.out.println(" The % that been Entered for First Row :"+firstinput+"First Column :"+s1); System.out.println(" The % that been Entered for Second Row :"+secondinput+"First Column :"+s2); System.out.println(" The % that been Entered for Third Row :"+thirdinput+"First Column :"+s3); System.out.println(); System.out.println("U(3) is :"+s1+" "+s2+" "+s3); System.out.println("Sum of U(3) is :" +sum); } else { System.out.println("P Value is Not Equal to 1 "); } } else System.out.println("Markov Chain Invalid Row not Equal to 1"); } }
Advertisement
Answer
You have problem with this code:
double m[][] = {{oneM, twoM, threeM, fourM, fiveM, sixM, sevenM, eightM, nineM}};
This is not matrix 3×3 but 1×9
Change code to this:
double m[][] = {{oneM, twoM, threeM}, {fourM, fiveM, sixM},{ sevenM, eightM, nineM}};