Skip to content

Instantly share code, notes, and snippets.

@davidbarsky
Created March 18, 2014 22:08
Show Gist options
  • Save davidbarsky/9630824 to your computer and use it in GitHub Desktop.
Save davidbarsky/9630824 to your computer and use it in GitHub Desktop.
Multiplies two matrices entered by a user. It's a fun math/logic problem to solve, especially with multiple levels of for loops.
import java.util.*;
import java.lang.Math.*;
public class MatrixMultiplier {
public static void main(String[] args) {
// Object Decleration
Scanner console = new Scanner(System.in);
// Following is for arrayA
System.out.println("This is for array A.");
// Amount of rows
System.out.println("Enter the number of rows:");
int a = console.nextInt();
// Amount of columns
System.out.println("Enter the amount of columns:");
int b = console.nextInt();
// Declares array
int[][] arrayA = new int[a][b];
// Fills array A
System.out.println("Enter the members of the array, seperated by a space.");
for (int i = 0; i < arrayA.length; i++) {
for (int j = 0; j < arrayA[0].length; j++) {
arrayA[i][j] = console.nextInt();
}
}
// Makes data entry more legible.
System.out.println();
// Following is for ArrayB
System.out.println("This is for array B.");
// Amount of rows
System.out.println("Enter the number of rows:");
int x = console.nextInt();
// Amount of columns
System.out.println("Enter the amount of columns:");
int y = console.nextInt();
// Declares array
int[][] arrayB = new int[x][y];
// Fills array b
System.out.println("Enter the members of the array, seperated by a space.");
for (int i = 0; i < arrayB.length; i++) {
for (int j = 0; j < arrayB[0].length; j++) {
arrayB[i][j] = console.nextInt();
}
}
// All array declarations are done in main method for organization. arrayC is therefore declared
int[][] arrayC = new int[a][y];
multiplier(a, y, x, arrayA, arrayB, arrayC);
// Prints out final array
System.out.println(Arrays.deepToString(arrayC));
}
public static void multiplier(int a, int y, int x, int[][] arrayA, int[][] arrayB, int[][] arrayC) {
for (int i = 0; i < a; ++i) {
for (int j = 0; j < y; ++j) {
for (int k = 0; k < x; ++k) {
arrayC[i][k] += arrayA[i][k] * arrayB[k][j];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment