Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Macierze
- * Opis:
- * Mnozenie dwóch macierzy
- * @author Ovitz
- */
- package macierze;
- import java.util.Scanner;
- public class Main
- {
- public static void main(String[] args)
- {
- float mac1[][],
- mac2[][],
- wynik[][] = null;
- System.out.println("Pierwsza macierz");
- mac1 = Wypelni();
- System.out.println("Druga macierz");
- mac2 = Wypelni();
- try
- {
- wynik = Mnoz( mac1, mac2 );
- }
- catch( Exception e)
- {
- System.out.println("Nie dało się pomnożyć");
- System.exit( 1 );
- }
- System.out.println("Wynik mnożenia");
- Pokaz( wynik );
- }
- // Funkcja zwraca wypelniona macierz #####
- private static float[][] Wypelni()
- {
- float macierz[][];
- int x, y;
- Scanner read = new Scanner( System.in );
- System.out.print("Podaj ilosc kolumn macierzy: ");
- x = read.nextInt();
- System.out.print("Podaj ilosc wierszy macierzy: ");
- y = read.nextInt();
- macierz = new float[x][y];
- for( x = 0; x < macierz.length; x++ )
- for( y = 0; y < macierz[x].length; y++ )
- {
- System.out.printf("[%d][%d]: ", x, y);
- macierz[x][y] = read.nextInt();
- }
- return macierz;
- }
- // #####
- // Funkcja wykonujaca mnozenie dwoch macierzy #####
- private static float[][] Mnoz( float mac1[][], float mac2[][] )
- {
- float macierz[][] = new float[mac1[0].length][mac2.length];
- float temp = 0;
- for( int x = 0; x < macierz.length; x++ )
- for( int y = 0; y < macierz[x].length; y++ )
- {
- for( int i = 0; i < mac1.length; i++ )
- if( x <= mac1.length && y <= mac2[0].length )
- temp += mac1[i][y] * mac2[x][i];
- macierz[x][y] = temp;
- temp = 0;
- }
- return macierz;
- }
- // #####
- // Funkcja pokazujaca podana macierz #####
- private static void Pokaz( float mac[][] )
- {
- for( int y = 0; y < mac[0].length; y++ )
- {
- for( int x = 0; x < mac.length; x++)
- System.out.printf("%10.2f", mac[x][y]);
- System.out.println();
- }
- }
- // #####
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement