-
Lenguaje
Java
-
Descripción
Multiplica dos matrices (A y B) de cualquier tamaño y guarda en resultado en otra matriz (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class MultiplicaMatrices {
public static void main(String[] args) {
int i, j, k, filas_a, columnas_a, filas_b, columnas_b;
Scanner in = new Scanner(System.in);
System.out.print("Ingrese el n\243mero de filas de la matriz A : ");
filas_a = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de columnas de la matriz A: ");
columnas_a = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de filas de la matriz B : ");
filas_b = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de columnas de la matriz B: ");
columnas_b = Integer.parseInt(in.nextLine());
if (filas_a<1 || columnas_a<1 || filas_b<1 || columnas_b<1 || columnas_a != filas_b)
System.err.println("\nLas matrices no se pueden multiplicar.");
else {
double[][] a = new double[filas_a][columnas_a];
double[][] b = new double[filas_b][columnas_b];
double[][] c = new double[filas_a][columnas_b];
System.out.println();
for (i=0; i<filas_a; i++)
for (j=0; j<columnas_a; j++) {
System.out.print("Ingrese el elemento A[" + i + "][" + j + "]: ");
a[i][j] = Double.parseDouble(in.nextLine());
}
System.out.println();
for (i=0; i<filas_b; i++)
for (j=0; j<columnas_b; j++) {
System.out.print("Ingrese el elemento B[" + i + "][" + j + "]: ");
b[i][j] = Double.parseDouble(in.nextLine());
}
for (i=0; i<filas_a; i++)
for (j=0; j<columnas_b; j++) {
c[i][j] = 0;
for (k=0; k<filas_b; k++)
c[i][j] += a[i][k] * b[k][j];
}
System.out.println("\nResultado:");
for (i=0; i<filas_a; i++) {
System.out.print("[\t");
for (j=0; j<columnas_b; j++)
System.out.print(c[i][j] + "\t");
System.out.println("]");
}
}
System.out.println();
}
}
public class MultiplicaMatrices {
public static void main(String[] args) {
int i, j, k, filas_a, columnas_a, filas_b, columnas_b;
Scanner in = new Scanner(System.in);
System.out.print("Ingrese el n\243mero de filas de la matriz A : ");
filas_a = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de columnas de la matriz A: ");
columnas_a = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de filas de la matriz B : ");
filas_b = Integer.parseInt(in.nextLine());
System.out.print("Ingrese el n\243mero de columnas de la matriz B: ");
columnas_b = Integer.parseInt(in.nextLine());
if (filas_a<1 || columnas_a<1 || filas_b<1 || columnas_b<1 || columnas_a != filas_b)
System.err.println("\nLas matrices no se pueden multiplicar.");
else {
double[][] a = new double[filas_a][columnas_a];
double[][] b = new double[filas_b][columnas_b];
double[][] c = new double[filas_a][columnas_b];
System.out.println();
for (i=0; i<filas_a; i++)
for (j=0; j<columnas_a; j++) {
System.out.print("Ingrese el elemento A[" + i + "][" + j + "]: ");
a[i][j] = Double.parseDouble(in.nextLine());
}
System.out.println();
for (i=0; i<filas_b; i++)
for (j=0; j<columnas_b; j++) {
System.out.print("Ingrese el elemento B[" + i + "][" + j + "]: ");
b[i][j] = Double.parseDouble(in.nextLine());
}
for (i=0; i<filas_a; i++)
for (j=0; j<columnas_b; j++) {
c[i][j] = 0;
for (k=0; k<filas_b; k++)
c[i][j] += a[i][k] * b[k][j];
}
System.out.println("\nResultado:");
for (i=0; i<filas_a; i++) {
System.out.print("[\t");
for (j=0; j<columnas_b; j++)
System.out.print(c[i][j] + "\t");
System.out.println("]");
}
}
System.out.println();
}
}