Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mnozeniemaceirzy;
- import static java.lang.System.*;
- import java.util.Scanner;
- import java.lang.ArrayIndexOutOfBoundsException;
- /**
- *
- * @author Hylus
- */
- public class Mnozeniemaceirzy {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //Scanner input = new Scanner (System.in);
- double[][] mac1;
- double[][] mac2;
- double[][] mac3=null;
- out.printf ("Wprowadz pierwszy macierz:");
- mac1 = Wprowadz();
- out.printf ("Wprowadz 2 macierz:");
- mac2 = Wprowadz();
- try
- {
- mac3 = PomnozMacierz (mac1,mac2);
- }
- catch (Exception e)
- {
- out.printf ("macierze maja inny rozmair\n");
- }
- WyswietlMacierz (mac3);
- try
- {
- out.printf ("Transponuje macierz\n");
- mac3 = TransponujMacierz (mac3);
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- out.printf ("Ta macierz nie istnieje\n");
- }
- WyswietlMacierz (mac3);
- }
- public static double[][] Wprowadz()
- {
- double[][] mac;
- int x=0, y=0;
- Scanner input = new Scanner (System.in);
- out.printf ("Podaj rozmiar macierzu (y i x) \n");
- x = input.nextInt();
- y = input.nextInt();
- mac = new double [x][y];
- out.printf ("Wprowadz dane:\n");
- for (int i=0; i<x; i++)
- {
- for (int j=0; j<y ; j++)
- {
- out.printf ("[%d][%d] - ",i,j);
- mac[i][j] = input.nextDouble();
- }
- }
- return mac;
- }
- public static double[][] PomnozMacierz( double[][] mac1,double[][] mac2)
- {
- double[][] mac3;
- mac3 = new double [0][0];
- if (mac1.length == mac2[0].length)
- {
- mac3 = new double [mac1.length][mac2[0].length];
- for (int y=0 ; y < mac1.length; y++)
- {
- for (int x=0; x < mac2[0].length; x++)
- {
- for (int i=0; i < mac1[0].length; i++)
- {
- mac3[y][x] = mac3[y][x] + mac1[y][i] * mac2[i][x];
- }
- }
- }
- }
- else
- {
- out.printf ("macierze nie sa tych samych rozmairow\n");
- }
- return mac3;
- }
- public static void WyswietlMacierz (double[][] mac)
- {
- out.printf ("Wyswietlam wynik mnozenia macierzy\n");
- for (int i=0; i<mac.length; i++)
- {
- for (int j=0; j<mac[0].length ; j++)
- {
- out.printf ("%.2f ",mac[i][j]);
- }
- out.printf ("\n");
- }
- }
- public static double[][] TransponujMacierz (double[][] macierz)
- {
- double[][] macwyjsciowa;
- macwyjsciowa = new double[macierz[0].length][macierz.length];
- for (int y=0; y< macierz[0].length; y++)
- {
- for (int x=0; x < macierz.length; x++ )
- {
- macwyjsciowa[y][x] = macierz[x][y];
- }
- }
- return macwyjsciowa;
- }
- }
Advertisement