Depth First Search problems - java

http://usacotraining.blogspot.com/2013/10/problem-142-clocks.html
I am attempting to use Depth First Search to solve this problem. I created a Node class based on a 3x3 array, and am using a visited array of size 4^9. However, this clearly is not working at all (it is not printing anything). My attempts of debugging (see comments) showed me that for some reason the array seems to be resetting each time, but I cannot figure out why.
Also, I am sure that my code is unnecessarily complicated, and there is a better way to perform DFS like this. Can someone show me a better way?
/*
ID: akshajk1
LANG: JAVA
TASK: clocks
*/
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
class clocks {
public static boolean[] visited;
public static boolean done;
public static String[] moves = {"ABDE", "ABC", "BCEF", "ADG", "BDEFH", "CFI", "DEGH", "GHI", "EFHI"};
public static Stack<Integer> ans = new Stack<Integer>();
public static Stack<Integer> finalans = new Stack<Integer>();
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// BufferedReader bf = new BufferedReader(new FileReader("clocks.in"));
// PrintWriter out = new PrintWriter(new FileWriter("clocks.out"));
// int n = Integer.parseInt(bf.readLine());
int[][] a = new int[3][3];
for(int i=0; i<3; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
for(int j=0; j<3; j++) a[i][j] = Integer.parseInt(st.nextToken());
}
visited = new boolean[4*4*4*4*4*4*4*4*4];
Node x = new Node(a);
dfs(x);
for(int i : finalans)
out.println(i + " ");
out.close(); System.exit(0);
}
public static void dfs(Node a) {
if(done) return;
int n = 0;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
n = 4*n+a.get(i, j);
if(visited[n]) return;
// for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(a.get(i, j)); System.out.print("\n"); } System.out.print("\n");
// System.out.println(Arrays.toString(ans.toArray()) + "\n");
boolean gotit = true;
for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(a.get(i, j) != 0) gotit = false;
if(gotit) {
for(int i : ans)
finalans.push(i);
done = true;
return;
}
visited[n] = true;
for(int i=0; i<moves.length; i++) {
char[] x = moves[i].toCharArray();
int[][] b = new int[3][3];
for(int k=0; k<3; k++)
for(int kk=0; kk<3; kk++)
b[k][kk] = a.get(k, kk);
Node abc = new Node(b);
for(char c : x)
abc = abc.turn(c);
ans.push((i+1));
dfs(abc);
}
ans.pop();
}
}
class Node {
final private int[][] a;
public Node(int[][] aaa) {
a = new int[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
a[i][j] = aaa[i][j];
}
public int get(int i, int j) {
return (a[i][j] % 12)/3;
}
public Node turn(char c) {
int val = c - 'A';
int[][] b = new int[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
b[i][j] = a[i][j];
b[val/3][val%3] += 3;
if(b[val/3][val%3] > 12) b[val/3][val%3] %= 12;
if(b[val/3][val%3] == 0) b[val/3][val%3] += 12;
Node ansans = new Node(b);
for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(ansans.get(i, j)); System.out.print("\n"); } System.out.print("\n");
return ansans;
}
}
edit: Thanks for all the help! I was able to correct the copying bug, and now the print statements make more sense, but the program is still stopping after performing move 1 four times. I changed the visited array to mark the initial value as true, resulting in the following code:
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
class clocks {
public static boolean[] visited;
public static boolean done;
public static String[] moves = {"ABDE", "ABC", "BCEF", "ADG", "BDEFH", "CFI", "DEGH", "GHI", "EFHI"};
public static Stack<Integer> ans = new Stack<Integer>();
public static Stack<Integer> finalans = new Stack<Integer>();
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// BufferedReader bf = new BufferedReader(new FileReader("clocks.in"));
// PrintWriter out = new PrintWriter(new FileWriter("clocks.out"));
// int n = Integer.parseInt(bf.readLine());
int[][] a = new int[3][3];
for(int i=0; i<3; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
for(int j=0; j<3; j++) a[i][j] = Integer.parseInt(st.nextToken());
}
visited = new boolean[4*4*4*4*4*4*4*4*4];
Node x = new Node(a);
int n = 0;
for(int xx=0; xx<3; xx++)
for(int j=0; j<3; j++)
n = 4*n+x.get(xx, j);
visited[n] = true;
dfs(x);
for(int i : finalans)
out.println(i + " ");
out.close(); System.exit(0);
}
public static void dfs(Node a) {
if(done) return;
// for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(a.get(i, j)); System.out.print("\n"); } System.out.print("\n");
// System.out.println(Arrays.toString(ans.toArray()) + "\n");
boolean gotit = true;
for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(a.get(i, j) != 0) gotit = false;
if(gotit) {
for(int i : ans)
finalans.push(i);
done = true;
return;
}
for(int i=0; i<moves.length; i++) {
char[] x = moves[i].toCharArray();
int[][] b = new int[3][3];
for(int k=0; k<3; k++) {
for(int kk=0; kk<3; kk++) {
b[k][kk] = a.get(k, kk)*3;
if(b[k][kk] == 0) b[k][kk] = 12;
}
}
Node abc = new Node(b);
for(char c : x)
abc = abc.turn(c);
int n = 0;
for(int xx=0; xx<3; xx++)
for(int j=0; j<3; j++)
n = 4*n+abc.get(xx, j);
if(!visited[n]) {
visited[n] = true;
ans.push((i+1));
dfs(abc);
ans.pop();
}
}
}
}
class Node {
final private int[][] a;
public Node(int[][] aaa) {
a = new int[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
a[i][j] = aaa[i][j];
}
public int get(int i, int j) {
return (a[i][j] % 12)/3;
}
public Node turn(char c) {
int val = c - 'A';
int[][] b = new int[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
b[i][j] = a[i][j];
b[val/3][val%3] += 3;
if(b[val/3][val%3] > 12) b[val/3][val%3] %= 12;
if(b[val/3][val%3] == 0) b[val/3][val%3] += 12;
Node ansans = new Node(b);
// for(int i=0; i<3; i++) { for(int j=0; j<3; j++) System.out.print(ansans.get(i, j)); System.out.print("\n"); } System.out.print("\n");
return ansans;
}
}
(I also fixed another bug). This for some reason is still not working, and is yielding a stack overflow error. I understand that there is a lot of recursion, but only a maximum of 9*3 = 27 moves should be made anyways. Why is this happening?

Your copying is wrong. The line where you copy a to b. You set each oft b's values to a.get(i, j) where a.get() does not return its value but value%12 divided by 3.
Ok i figured out that for the first 6000 searches (thats when your ans-stack overflows) the algorithm always has a valid move to choose.
To prevent that you should consider breadth first search or set a maximum search depth.
I´ve added a maximum search depth and the algorithm returns, however the turns required change from situation to situation and range over a large set. Therefore sometimes 200 moves are required, so you can´t set a maximum search depth of any lower value. But then with 200 as maximum search depth. Any 200 step solution will be accepted even if better solutions exist.
My recommendation therefore is to take depth first search as your search algorithm.

Related

How can I sort using two different packages?

I had an assignment for a class where I had to develop a simple number sort program.
My main is supposed to receive the user input and my sort class is supposed to interrupt and spit out the resulting numbers in ascending and descending order. The problem is that my main is taking the input but it's not putting it in order at all and I'm unsure why.
package main;
import sort.Sort;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int arr[] = new int[5];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<5; i++) {
System.out.print("Enter a number: ");
myScanner.nextLine();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
}
package sort;
public class Sort {
public void ascendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void descendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
I know I'm missing something in my Main, because I'm fairly certain I don't need to put anything else into the sort class.
You're code is perfect. You just missed one assignment in your main method.
Instead of myScanner.nextLine(); you should have written arr[i] = myScanner.nextLine();
myScanner.nextLine(); is getting the next line you enter in the console but it isn't saving it anywhere. arr[i] = myScanner.nextLine(); will save the value of the console in the arr array.
Everything else should work after that.
First you are getting user input using myScanner.nextLine() but not storing it. myScanner.nextLine() won't save anything which user enters .
Thus you need to store it in an array and then use it later. So your main method after the changes should be like this.
public static void main(String[] args) {
int arr[] = new int[3];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<3; i++) {
System.out.print("Enter a number: ");
arr[i] = myScanner.nextInt();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
And your sorting functions are also wrong. There you do some mis-calculations and return / log nothing. Thus you will not see anything in the console if you are not logging or returning anything.
public void descendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
public void ascendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
I hope this functions will work for your use case of ascending and descending sort.

How can I check if every single int in a randomly generated array is even and make it create another random array if it's not?

So I'm trying to create a program that creates a randomly generated array with numbers between 0 and 10.
Every time a number inside the 4x4 array is odd I want it to generate a brand new array and print every array discarded aswell until it creates a 4x4 array with only even numbers.
The problem right now is that I can't understand how to fix the last for and make it work properly with the boolean b that is supposed to restart the creation of the array.
import java.util.Scanner;
public class EvenArrayGenerator {
public static void main(String a[]) {
Boolean b;
do {
b = true;
int[][] Array = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
Array[i][j] = (int) (Math.random() * 11);
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (Array[i][j] % 2 != 0)
b = false;
}
}
} while (b);
}
}
public class ArrayGen {
private int[][] array = new int[4][4];
private int iterations = 1; // you always start with one iteration
public static void main (String[] args) {
ArrayGen ag = new ArrayGen();
ag.reScramble();
while(!ag.isAllEven()) {
ag.reScramble();
ag.iterations++;
}
// this is just a nice visualisation
for (int i = 0; i < 4; i++) {
System.out.print("[");
for (int j = 0; j < 4; j++) {
System.out.print(ag.array[i][j] +((j != 3)? ", " : ""));
}
System.out.print("]\n");
}
System.out.println(ag.iterations + " iterations needed to get all-even array.");
}
private void reScramble () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array[i][j] = (int)(Math.random() * 11);
}
}
}
private boolean isAllEven () {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (array[i][j] % 2 == 1) {
return false;
}
}
}
return true;
}
}
I think this is a good solution. Refactoring your code into structured methods is never a bad idea. I hope this helps!
You are looping until you get an array that's all even. You should initialize b to be false, and update it to true in the (nested) for loop. Note that once's you've set it to false, there's no reason checking the other members of the array, and you can break out of the for loop.
Note, also, that using stream could make this check a tad more elegant:
b = Arrays.stream(arr).flatMapToInt(Arrays::stream).anyMatch(x -> x % 2 != 0)
What about generating random numbers up to 5 and double it? Then you don't have two check if they are even.
Instead of your last for loop:
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(Array[i][j] % 2!=0){
b=false;
break;
}
}
if(!b){
break;
}
}
if(!b){
break;
}
Alternatively, you could do an oddity check when you are generating the elements. Something like:
int element;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
do{
element = (int)(Math.random()*11);
}while(element % 2 !=0)
Array[i][j] = element;
}
}
That way you don't have to check the values, they will always be even.
This should work:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
anyOdd |= Array[i][j] % 2!=0;
}
}
} while(anyOdd);
}
}
As you can see, I just modified the condition from b to anyOdd, so if there is any odd number, it will iterate again.
Also, you can check it when you generate the random numbers, so you avoid a second loop:
import java.util.Scanner;
public class EvenArrayGenerator{
public static void main(String a[]){
boolean anyOdd;
int array = 0;
do{
System.out.println ("Array " + ++array + ":");
anyOdd=false;
int[][] Array = new int[4][4];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
Array[i][j] = (int)(Math.random()*11);
anyOdd |= array[i][j] % 2 != 0;
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(Array[i][j] + " ");
}
System.out.println();
}
} while(anyOdd);
}
}
public class EvenArrayGenerator {
public static void main(String a[]) {
int[][] arr = createAllEvenArray(4);
printArray(arr);
}
private static int[][] createAllEvenArray(int size) {
while (true) {
int[][] arr = createArray(size);
printArray(arr);
if (isAllEven(arr))
return arr;
}
}
private static int[][] createArray(int size) {
int[][] arr = new int[size][size];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
arr[i][j] = (int)(Math.random() * 11);
return arr;
}
private static void printArray(int[][] arr) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j > 0)
System.out.print("\t");
System.out.format("%2d", arr[i][j]);
}
System.out.println();
}
System.out.println();
}
private static boolean isAllEven(int[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
if (arr[i][j] % 2 != 0)
return false;
return true;
}
}

ArrayIndexOutOfBoundsException when trying to find the determinant of a 2D Array/Matrix

this is my first Stack Overflow post, and I am fairly new to java, so I may not initially comprehend some of the feedback you give me.
With this program, I am supposed to find the determinant of a matrix recursively with a size determined by the user. When I do so, however, I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Determinant.Copy<Determinant.java:55>
at Determinant.det<Determinant.java:31>
at Determinant.main<Determinant.java:15>
I understand what this error means, but I don't understand why it's happening.
Here are the classes I am using (both printmatrix and the main method were written by my teacher, I had to complete the Copy and det methods):
import javax.swing.JOptionPane;
public class Determinant
{
public static void main(String args[])
{
String sizeStr = JOptionPane.showInputDialog("What size?");
int size = Integer.parseInt(sizeStr);
int[][] matrix = new int[size][size];
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
matrix[i][j] = (int)(Math.random()*40)-20;
printArray(matrix);
System.out.println("\nThe determinant = "+det(matrix));
}
public static int det(int[][] A)
{
int answer = 0;
int place = 0;
int[][] temp;
int[][] temp1;
if(A.length==1){
return(A[0][0]);
}
for(int i = 0; i<A.length; i++){
temp = new int[A.length-1][A[0].length-1];
temp1 = Copy(temp, i);
if(i%2==0){
place = 1;
}
else{
place = -1;
}
answer = answer + place * A[0][i] * det(temp1);
}
return answer;
}
public static int[][] Copy(int[][] B, int i)
{
int[][] C = new int[B.length-1][B.length-1];
for(int j = 1; j<B.length; j++){
for(int k = 0; k<B[0].length; k++){
if(k>i){
C[j-1][k-1]=B[j][k];
}
else{
C[j-1][k]=B[j][k];
}
}
}
return C;
}
public static void printArray(int[][] A)
{
for(int i=0; i<A.length; i++)
{
for(int j=0; j<A.length; j++)
{
int num = A[i][j];
if(num<-9)
System.out.print(" ");
else if(num<0||num>9)
System.out.print(" ");
else
System.out.print(" ");
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
The error occurs at the else statement in Copy and temp1 = Copy(temp, i).
I am confused, as if either j or k = 1, shouldn't that be a position in the array? What am I missing?
The size of C array in method copy, should be same as B array.
Reason: You are copying B array into C array, they should have same size.
Try the following:
import javax.swing.JOptionPane;
public class Determinant
{
public static void main(String args[])
{
String sizeStr = JOptionPane.showInputDialog("What size?");
int size = Integer.parseInt(sizeStr);
int[][] matrix = new int[size][size];
for(int i=0; i<size; i++) {
for(int j=0; j<size; j++) {
matrix[i][j] = (int) (Math.random() * 40) - 20;
}
}
printArray(matrix);
System.out.println("\nThe determinant = "+det(matrix));
}
public static int det(int[][] A)
{
int answer = 0;
int place = 0;
int[][] temp;
int[][] temp1;
if(A.length==1){
return(A[0][0]);
}
for(int i = 0; i<A.length; i++){
temp = new int[A.length-1][A[0].length-1];
temp1 = Copy(temp, i);
if(i%2==0){
place = 1;
}
else{
place = -1;
}
answer = answer + place * A[0][i] * det(temp1);
}
return answer;
}
public static int[][] Copy(int[][] B, int i)
{
//The C array size should be same as B
int[][] C = new int[B.length][B[0].length];
for(int j = 1; j<B.length; j++){
for(int k = 0; k<B[0].length; k++){
if(k>i){
C[j-1][k-1]=B[j][k];
}
else{
C[j-1][k]=B[j][k];
}
}
}
return C;
}
public static void printArray(int[][] A)
{
for(int i=0; i<A.length; i++)
{
for(int j=0; j<A.length; j++)
{
int num = A[i][j];
if(num<-9)
System.out.print(" ");
else if(num<0||num>9)
System.out.print(" ");
else
System.out.print(" ");
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
Hope this explains, enjoy!
You should declare your matrix in Copy function in this way:
int[][] C = new int[B.length][B[0].length];
Otherwise you are declaring a matrix without a row and column. The fact that you start to use from 0 doesn't mean you should declare one row in less!

Generate rotations of a string, java

Suppose, I have a string "big$". I need the following :
big$
ig$b
g$bi
$big
I also need to store these strings in an ArrayList.
Can someone please help me. I am unable to think of a solution.
This is what I have done. I am able to print it but I don't know how to store them.
private static void rotations(String str) {
int n = str.length();
char temp;
String temp1 = null;
for(int i=0; i<n; i++){
System.out.println();
for(int j=i+1; j<n; j++)
System.out.print(str.charAt(j));
for(int k=0; k<=i; k++)
System.out.print(str.charAt(k));
}
}
You are very close, just instead of printing out each char append it to a String variable inside your loop, then add that variable to an ArrayList<String>
private static List<String> rotations(String str) {
int n = str.length();
char temp;
String temp1 = null;
List<String> retval = new ArrayList<String>();
for(int i=0; i<n; i++){
StringBuilder sb = new StringBuilder();
for(int j=i+1; j<n; j++)
sb.append(str.charAt(j));
for(int k=0; k<=i; k++)
sb.append(str.charAt(k));
retval.add(sb.toString());
}
return retval;
}
And you can print those out in your main method:
public static void main (String[] args)
{
List<String> rotations = rotations("big$");
for(String s : rotations)
{
System.out.println(s);
}
}
Try this code:
String a = "big$";
char[] array = a.toCharArray();
List<String> list = new ArrayList<String>();
for(int i = 0; i < array.length; i++)
{
String temp = "" + array[i];
for( int j = i+1; j < array.length + i; j++)
{
temp += array[j%(array.length)];
}
list.add(temp);
}
A simple sample of ArrayList:
ArrayList<String> container = new ArrayList<String>();
container.add("A");
container.add("B");
container.add("C");
for (int i = 0; i < container.size(); i++) {
System.out.println(container.get(i));
}

Printing two 2D Arrays Next to Each Other

So I've been fiddling around with 2D arrays in java. I am practicing by using the arrays as matrices. I create 2 Matrix objects, which have a myMatrix 2D array field. In the main method, I call the first Matrix object to add itself to the second matrix if it is possible, through the addMatrix method. I have a printResultDetail method to actually print out what is happening. I want the first object's myMatrix to print out with rows and columns properly formatted. Then I want the second object's myMatrix object to print out on the right of the previous. I then want the new output matrix, given by the first object's myResultMatrix 2D array, to printed out again to the right.
How do I make the arrays print out side by side?
Note: the printResultDetail is incorrect. I was just trying to figure out how to do it.
import java.util.Arrays;
import java.util.Random;
public class Matrix {
int[][] myMatrix;
int[][] myResultMatrix;
int myMatrixRow;
int myMatrixCol;
Random rand = new Random();
public Matrix(int rowSize, int colSize, int maxVal, int minVal){
myMatrixRow = rowSize;
myMatrixCol = colSize;
myMatrix = new int[rowSize][colSize];
for(int i = 0; i < rowSize; i++){
for(int k = 0; k < colSize; k++){
myMatrix[i][k] = rand.nextInt((maxVal - minVal) + 1) + minVal; //assigns each part of array to rand #
}
}
for(int i = 0; i<myMatrixRow; i++){
System.out.print("[");
for(int j = 0; j<myMatrixCol; j++){
System.out.print(" " + myMatrix[i][j] + " ");
}
System.out.print("]");
System.out.println();
}
//System.out.println(Arrays.deepToString(myMatrix));
}
public int[][] multMatrix(Matrix matrix2){
if(canMultiply(matrix2) == true){
myResultMatrix = new int[myMatrixRow][matrix2.myMatrixCol];
for (int i = 0; i < myMatrixRow; i++) {
for (int j = 0; j < matrix2.myMatrixCol; j++) {
for (int k = 0; k < myMatrixCol; k++) {
myResultMatrix[i][j] += myMatrix[i][k] * matrix2.myMatrix[k][j];
}
}
}
return myResultMatrix;
}else{
myResultMatrix = null;
return null;
}
}
public boolean canMultiply(Matrix matrix2){ //can only multiply if the columns of
//first matrix is equal to the rows of the second
if(myMatrixCol == matrix2.myMatrixRow){
return true;
}else{
return false;
}
}
public int[][] addMatrix(Matrix matrix2){
if(myMatrixRow == matrix2.myMatrixRow && myMatrixCol == matrix2.myMatrixCol){
myResultMatrix = new int[myMatrixRow][myMatrixCol];
for(int i = 0; i < myMatrixRow; i++){
for(int k = 0; k < myMatrixCol; k++){
myResultMatrix[i][k] = myMatrix[i][k] + matrix2.myMatrix[i][k];
}
}
return myResultMatrix;
}else{
myResultMatrix = null;
return null;
}
}
public void printResultDetail(Matrix matrix2){
for(int i = 0; i<myMatrixRow; i++){
System.out.print("[");
for(int j = 0; j<myMatrixCol; j++){
System.out.print(" " + myMatrix[i][j] + " ");
}
System.out.print("] ");
System.out.println();
for(int k = 0; k<matrix2.myMatrixRow; k++){
System.out.print("[");
for(int x = 0; x<matrix2.myMatrixCol; x++){
System.out.print(" " + matrix2.myMatrix[k][x] + " ");
}
System.out.print("]");
}
}
}
public static void main(String[] args){
Matrix firstMatrix = new Matrix(3, 3, 5, 1);
Matrix secondMatrix = new Matrix(3, 3, 5, 1);
System.out.println(Arrays.deepToString(firstMatrix.addMatrix(secondMatrix)));
//System.out.println(Arrays.deepToString(firstMatrix.addMatrix(secondMatrix)));
}
}
This code below will print:
maybe you can use as sample.
public class Test {
public static void main(String[] args) {
double[][] matrixLeft = {{1,5,2,8,4,70,55,80},{3,7,4,2,6,60,30,70}};
double[][] matrixRight = {{8,1,6,4,2,10,40,60},{1,5,2,8,4,70,50,80},{3,7,4,2,6,60,30,70}};
int endOfLoop = matrixLeft.length > matrixRight.length ? matrixLeft.length : matrixRight.length;
for(int i = 0; i < endOfLoop; i++){
if(matrixLeft.length > i){
printLine(matrixLeft[i]);
System.out.print(" ");
} else {
printBlankLine(matrixLeft[0].length);
}
if(matrixRight.length > i){
printLine(matrixRight[i]);
}
System.out.println();
}
}
private static void printLine(double[] line){
for(double number : line){
System.out.print(number + " ");
}
}
private static void printBlankLine(int lenght){
for(int i=0; i < lenght; i++){
System.out.print(" ");
}
}
}

Categories