Advertisement
elizabethdutton

Untitled

Oct 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1.  
  2. /**********************************************************************************************
  3. * @file MatrixMultiply.java
  4. * @brief this is a program that multiplies two matrices together
  5. * @author Elizabeth Dutton
  6. * @data October 17, 2019
  7. **********************************************************************************************/
  8.  
  9. import java.util.Scanner;
  10. public class MatrixMultiply {
  11. public static void main(String[] args) {
  12. Scanner keyboard = new Scanner(System.in); //allows me to use scanners from user
  13. System.out.println("Enter the number of rows and columns for the first matrix:");
  14. int matrixOneRow; //rows of first matrix
  15. int matrixOneColumn; //rows of second matrix
  16. matrixOneRow = keyboard.nextInt(); //gets rows from user
  17. matrixOneColumn = keyboard.nextInt(); //gets columns from user
  18.  
  19. // variables for for loops
  20. int i;
  21. int j;
  22. int k;
  23.  
  24.  
  25. int[][] matrixOne = new int[matrixOneRow][matrixOneColumn]; //a matrix with the rows and columns from user
  26. System.out.println("Enter the elements of the first matrix:");
  27. for (i=0; i<matrixOneRow; i++){ //keeps it within the row
  28. for (j=0; j<matrixOneColumn; j++) { //keeps it within the column
  29. matrixOne[i][j]= keyboard.nextInt(); // stores the matrix values from user
  30. }
  31.  
  32. }
  33.  
  34. System.out.println("Enter the number of rows and columns for the second matrix:");
  35. int matrixTwoRow; //rows of first matrix
  36. int matrixTwoColumn; // rows of second matrix
  37. matrixTwoRow = keyboard.nextInt(); //gets rows from user
  38. matrixTwoColumn = keyboard.nextInt(); //gets columns from user
  39.  
  40.  
  41.  
  42. int[][] matrixTwo = new int[matrixTwoRow][matrixTwoColumn]; //a matrix with the rows and columns from user
  43. System.out.println("Enter the elements of the second matrix:");
  44. for (i=0; i<matrixTwoRow; i++){ //keeps it within the row
  45. for (j=0; j<matrixTwoColumn; j++) { //keeps it within the column
  46. matrixTwo[i][j]= keyboard.nextInt(); //stores the matrix values from user
  47. }
  48.  
  49. }
  50.  
  51. //if the columns of the first matrix and the rows of the second matrix do not match, the program should not run
  52. if (matrixOneColumn != matrixTwoRow) {
  53. System.out.println("Please restart and enter another matrix, these values cannot multiply");
  54.  
  55. }
  56.  
  57. int[][] finalMatrix = new int [matrixOneRow][matrixTwoColumn]; //combines the multiplied matrix
  58.  
  59. for(i=0; i<matrixOneRow; i++) { //keeps it within the first matrix rows
  60. for (j=0; i<matrixTwoColumn; j++) { // keeps it within the second matrix columns
  61. for (k=0; k<matrixOneColumn; k++){ //keeps the matrices in the right place
  62. finalMatrix[i][j] = (matrixOneRow * matrixOneColumn) + finalMatrix[i][j] ; // multiplying rows and columns and adding it to the stored value
  63.  
  64. }
  65. System.out.print(finalMatrix[i][j] + "\t"); //prints out matrix in a organized way
  66. }
  67. System.out.println(""); //goes to new line when first row is done
  68. }
  69.  
  70. /** for( i=0; i<finalMatrix.length; i++){
  71. for (j=0; j<finalMatrix[i].length; j++) {
  72. System.out.print(finalMatrix[i][j] + "\t"); //prints out matrix in a organized way
  73.  
  74. }
  75. // System.out.println(""); //goes to new line when first row is done
  76. }
  77. **/
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. // }
  91.  
  92. // start with columns of first matrix = rows of second matrix
  93. // resulting matrix is rows of first matrix by columns of second matrix
  94. // calculate down on first matrix times across second matrix
  95. //multiply column of the first matrix times every row of the second matrix
  96. // 3x3 4x2 2x5 try all, make sure some dont work and its error
  97.  
  98. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement