Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /**
  2. * Macierze
  3. * Opis:
  4. * Mnozenie dwóch macierzy
  5. * @author Ovitz
  6. */
  7. package macierze;
  8. import java.util.Scanner;
  9.  
  10. public class Main
  11. {
  12. public static void main(String[] args)
  13. {
  14. float mac1[][],
  15. mac2[][],
  16. wynik[][] = null;
  17. System.out.println("Pierwsza macierz");
  18. mac1 = Wypelni();
  19. System.out.println("Druga macierz");
  20. mac2 = Wypelni();
  21. try
  22. {
  23. wynik = Mnoz( mac1, mac2 );
  24. }
  25. catch( Exception e)
  26. {
  27. System.out.println("Nie dało się pomnożyć");
  28. System.exit( 1 );
  29. }
  30. System.out.println("Wynik mnożenia");
  31. Pokaz( wynik );
  32. }
  33.  
  34. // Funkcja zwraca wypelniona macierz #####
  35. private static float[][] Wypelni()
  36. {
  37. float macierz[][];
  38. int x, y;
  39. Scanner read = new Scanner( System.in );
  40. System.out.print("Podaj ilosc kolumn macierzy: ");
  41. x = read.nextInt();
  42. System.out.print("Podaj ilosc wierszy macierzy: ");
  43. y = read.nextInt();
  44.  
  45. macierz = new float[x][y];
  46. for( x = 0; x < macierz.length; x++ )
  47. for( y = 0; y < macierz[x].length; y++ )
  48. {
  49. System.out.printf("[%d][%d]: ", x, y);
  50. macierz[x][y] = read.nextInt();
  51. }
  52. return macierz;
  53. }
  54. // #####
  55.  
  56. // Funkcja wykonujaca mnozenie dwoch macierzy #####
  57. private static float[][] Mnoz( float mac1[][], float mac2[][] )
  58. {
  59. float macierz[][] = new float[mac1[0].length][mac2.length];
  60. float temp = 0;
  61.  
  62. for( int x = 0; x < macierz.length; x++ )
  63. for( int y = 0; y < macierz[x].length; y++ )
  64. {
  65. for( int i = 0; i < mac1.length; i++ )
  66. if( x <= mac1.length && y <= mac2[0].length )
  67. temp += mac1[i][y] * mac2[x][i];
  68. macierz[x][y] = temp;
  69. temp = 0;
  70. }
  71. return macierz;
  72. }
  73. // #####
  74.  
  75. // Funkcja pokazujaca podana macierz #####
  76. private static void Pokaz( float mac[][] )
  77. {
  78. for( int y = 0; y < mac[0].length; y++ )
  79. {
  80. for( int x = 0; x < mac.length; x++)
  81. System.out.printf("%10.2f", mac[x][y]);
  82. System.out.println();
  83. }
  84. }
  85. // #####
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement