Advertisement
nadezhdavh

Matrices

Feb 26th, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import java.util.Scanner;
  2. //Same thing
  3. //More practice of functions and parameters
  4. public class Matrices {
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. System.out.println("Enter number of rows: ");
  8. int numRows = in.nextInt();
  9. System.out.println("Enter number of columns: ");
  10. int numColumns = in.nextInt();
  11. if ((numRows > 0 && numRows < 10) && ((numColumns > 0 && numColumns < 10))) {
  12. if (numRows <= numColumns) {
  13. int[][] matrix1 = new int[numRows][numColumns];
  14. int[][] matrix2 = new int[numRows][numColumns];
  15. int[][] empty = new int[numRows][numColumns];
  16.  
  17. for (int i = 0; i < empty.length; i++) {
  18. for (int j = 0; j < empty.length; j++) {
  19. empty[i][j] = 0;
  20. }
  21. }
  22.  
  23. System.out.println("Elements of first matrix: ");
  24. for (int i = 0; i < numRows; i++) {
  25. for (int j = 0; j < numColumns; j++) {
  26. matrix1[i][j] = in.nextInt();
  27. }
  28. }
  29. System.out.println("Elements of second matrix");
  30. for (int i = 0; i < numRows; i++) {
  31. for (int j = 0; j < numColumns; j++) {
  32. matrix2[i][j] = in.nextInt();
  33. }
  34. }
  35.  
  36. System.out.println("First matrix");
  37. printMatrix(matrix1);
  38. System.out.println("Second matrix: ");
  39. printMatrix(matrix2);
  40. System.out.println("Sum: ");
  41. int[][] sum = sumMatrix(matrix1, matrix2);
  42. printMatrix(sum);
  43. System.out.println("First next to the Second");
  44. printTwoMatrices(matrix1, matrix2);
  45. System.out.println("Direct: ");//as much as it may be called "direct"
  46. printTwoMatrices(matrix1, empty);
  47. printTwoMatrices(empty, matrix2);
  48.  
  49. } else {
  50. System.out.println("Number of Rows less or equal to Number of Columns");
  51. }
  52.  
  53. } else {
  54. System.out.println("Invalid!");
  55. }
  56. }
  57.  
  58.  
  59. public static void printMatrix(int[][] matrix) {
  60. for (int[] ints : matrix) {
  61. for (int column = 0; column < matrix[0].length; column++) {
  62. System.out.print(" " + ints[column] + " ");
  63. }
  64. System.out.println();
  65. }
  66. }
  67.  
  68. private static int[][] sumMatrix(int[][] first, int[][] second) {
  69. int rows = first.length;
  70. int columns = first[0].length;
  71. int[][] sum = new int[rows][columns];
  72. for (int row = 0; row < rows; row++) {
  73. for (int column = 0; column < columns; column++) {
  74. sum[row][column] = first[row][column] + second[row][column];
  75. }
  76. }
  77. return sum;
  78. }
  79.  
  80. public static void printTwoMatrices(int[][] first, int[][] second) {
  81. for (int i = 0; i < first.length; i++) {
  82. for (int j = 0; j < first[i].length; j++) {
  83. System.out.print(" " + first[i][j] + " ");
  84. }
  85. for (int j = 0; j < second[i].length; j++) {
  86. System.out.print(" " + second[i][j] + " ");
  87. }
  88. System.out.println();
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement