# Rešitev: program Produkt matrik (Java)

Različica od 15:29, 4 oktober 2006; poglej trenutno različico
←Starejša različica | Novejša različica→

` import java.io.*; public class ZmnoziMatrike{   public static void main(String[] args) throws IOException   {      BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));       System.out.println("Vnesi podatke o prvi matriki: ");      System.out.print("   Vnesi stevilo vrstic: ");      int n1 = Integer.parseInt(vhod.readLine());      System.out.print("   Vnesi stevilo stolpcev: ");      int m1 = Integer.parseInt(vhod.readLine());      int[][] mat1 = new int[n1][m1];      for (int i=0; i<n1; ++i) {         System.out.println("   Vnesi "+(i+1)+". vrstico: ");         for (int j=0; j<m1; ++j) {            System.out.print("      Vnesi "+(j+1)+". element: ");            int a = Integer.parseInt(vhod.readLine());            mat1[i][j]=a;         }      }       System.out.println("Vnesi podatke o drugi matriki: ");      System.out.print("   Vnesi stevilo vrstic: ");      int n2 = Integer.parseInt(vhod.readLine());      System.out.print("   Vnesi stevilo stolpcev: ");      int m2 = Integer.parseInt(vhod.readLine());      int[][] mat2 = new int[n2][m2];      for (int i=0; i<n2; ++i) {         System.out.println("   Vnesi "+(i+1)+". vrstico: ");         for (int j=0; j<m2; ++j) {            System.out.print("      Vnesi "+(j+1)+". element: ");            int b = Integer.parseInt(vhod.readLine());            mat2[i][j]=b;         }      }       int c=0;      int d=0;      if (m1==n2) {         int[][] mat3 = new int[n1][m2];         for (int i=0; i<m2; ++i) {            for (int j=0; j<n1; ++j) {               for (int k=0; k<m1; ++k) {                  d=(mat1[j][k])*(mat2[k][i]);                  c=c+d;               }               mat3[j][i]=c;               c=0;            }         }         System.out.println("Nova matrika: ");         System.out.println("   ");         for (int i=0; i<n1; ++i) {            for (int j=0; j<m2; ++j) {               System.out.print(+mat3[i][j]+" ");            }            System.out.println();         }      }       else System.out.println("Matrik ni mogoce zmnoziti.");   }} `