Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

import java.io.

*;
class MnozenjeMatrica {
public static void main(String[]args) throws IOException {

int i, j, k;

BufferedReader ulaz = new BufferedReader(

new InputStreamReader(System.in));

System.out.print("Broj redaka matrice A: " );


int m = Integer.parseInt(ulaz.readLine());

System.out.print("Broj stupaca matrice A: " );


int n = Integer.parseInt(ulaz.readLine());

double a[][] = new double [m][n];

for (i=0; i<m; i=i+1) {


for (j=0; j<n; j=j+1) {
System.out.print("element a"+(i+1)+(j+1)+": ");
a[i][j] = Double.parseDouble(ulaz.readLine());
}
}

System.out.println("\nBroj redaka matrice B je jednak broju stupaca matrice


A." );

System.out.print("Broj stupaca matrice B: " );


int p = Integer.parseInt(ulaz.readLine());

double b[][] = new double [n][p];

for (i=0; i<n; i=i+1) {


for (j=0; j<p; j=j+1) {
System.out.print("element b"+(i+1)+(j+1)+": ");
b[i][j] = Double.parseDouble(ulaz.readLine());
}
}

double c[][] = new double [m][p];

for (i=0; i <= m-1; i++)


for (k=0; k <= p-1; k++){
c[i][k] = 0.0;
for (j=0; j<= n-1; j++)
c[i][k] = c[i][k] + a[i][j] * b[j][k];
}

System.out.println("\nElementi matrice AB:");


for (i=0; i <= m-1; i++){
for (k=0; k <= p-1; k++)
System.out.print(c[i][k] + " ");
System.out.println();
}
}}

/*
Primjer:

Broj redaka matrice A: 2


Broj stupaca matrice A: 3
element a11: 1
element a12: 3
element a13: 2
element a21: 0
element a22: 2
element a23: 1

Broj redaka matrice B je jednak broju stupaca matrice A.


Broj stupaca matrice B: 4
element b11: 1
element b12: 1
element b13: 3
element b14: 2
element b21: 4
element b22: 0
element b23: 5
element b24: 1
element b31: 2
element b32: 3
element b33: 2
element b34: 0

Elementi matrice AB:


17.0 7.0 22.0 5.0
10.0 3.0 12.0 2.0

*/

You might also like