Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************************************
- * @file MatrixMultiply.java
- * @brief this is a program that multiplies two matrices together
- * @author Elizabeth Dutton
- * @data October 17, 2019
- **********************************************************************************************/
- import java.util.Scanner;
- public class MatrixMultiply {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in); //allows me to use scanners from user
- System.out.println("Enter the number of rows and columns for the first matrix:");
- int matrixOneRow; //rows of first matrix
- int matrixOneColumn; //rows of second matrix
- matrixOneRow = keyboard.nextInt(); //gets rows from user
- matrixOneColumn = keyboard.nextInt(); //gets columns from user
- // variables for for loops
- int i;
- int j;
- int k;
- int[][] matrixOne = new int[matrixOneRow][matrixOneColumn]; //a matrix with the rows and columns from user
- System.out.println("Enter the elements of the first matrix:");
- for (i=0; i<matrixOneRow; i++){ //keeps it within the row
- for (j=0; j<matrixOneColumn; j++) { //keeps it within the column
- matrixOne[i][j]= keyboard.nextInt(); // stores the matrix values from user
- }
- }
- System.out.println("Enter the number of rows and columns for the second matrix:");
- int matrixTwoRow; //rows of first matrix
- int matrixTwoColumn; // rows of second matrix
- matrixTwoRow = keyboard.nextInt(); //gets rows from user
- matrixTwoColumn = keyboard.nextInt(); //gets columns from user
- int[][] matrixTwo = new int[matrixTwoRow][matrixTwoColumn]; //a matrix with the rows and columns from user
- System.out.println("Enter the elements of the second matrix:");
- for (i=0; i<matrixTwoRow; i++){ //keeps it within the row
- for (j=0; j<matrixTwoColumn; j++) { //keeps it within the column
- matrixTwo[i][j]= keyboard.nextInt(); //stores the matrix values from user
- }
- }
- //if the columns of the first matrix and the rows of the second matrix do not match, the program should not run
- if (matrixOneColumn != matrixTwoRow) {
- System.out.println("Please restart and enter another matrix, these values cannot multiply");
- }
- int[][] finalMatrix = new int [matrixOneRow][matrixTwoColumn]; //combines the multiplied matrix
- for(i=0; i<matrixOneRow; i++) { //keeps it within the first matrix rows
- for (j=0; i<matrixTwoColumn; j++) { // keeps it within the second matrix columns
- for (k=0; k<matrixOneColumn; k++){ //keeps the matrices in the right place
- finalMatrix[i][j] = (matrixOneRow * matrixOneColumn) + finalMatrix[i][j] ; // multiplying rows and columns and adding it to the stored value
- }
- System.out.print(finalMatrix[i][j] + "\t"); //prints out matrix in a organized way
- }
- System.out.println(""); //goes to new line when first row is done
- }
- /** for( i=0; i<finalMatrix.length; i++){
- for (j=0; j<finalMatrix[i].length; j++) {
- System.out.print(finalMatrix[i][j] + "\t"); //prints out matrix in a organized way
- }
- // System.out.println(""); //goes to new line when first row is done
- }
- **/
- }
- }
- // }
- // start with columns of first matrix = rows of second matrix
- // resulting matrix is rows of first matrix by columns of second matrix
- // calculate down on first matrix times across second matrix
- //multiply column of the first matrix times every row of the second matrix
- // 3x3 4x2 2x5 try all, make sure some dont work and its error
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement