lunes, 21 de junio de 2021

Multiplicación de matrices en JAVA

package multi_matrices;
import java.util.*;
public class Mult2matrices {
static int fm=3;
static int cm=4;
static int cn=3;

    static int M[][] = new int[fm][cm];
    static int N[][] = new int[cm][cn];
    static int R[][] = new int[fm][cn];
    static Scanner tec = new Scanner(System.in);
  
public static void main(String[] args) {
int sw=0;
int opc=0;
while (sw==0) {
System.out.println("======================================");
System.out.println("======MULTIPLICACION DE MATRICES======");
System.out.println("======================================");
System.out.println("REGLA BÁSICA: LAS COLUMNAS DE LA PRIMERA");
System.out.println("MATRIZ DEBEN SER IGUALES A LAS FILAS DE ");
System.out.println("LA SEGUNDA MATRIZ. LA MATRIZ RESULTANTE ");
System.out.println("ESTA FORMADA POR LAS FILAS DE LA PRIMERA");
System.out.println("Y LAS COLUMNAS DE LA SEGUNDA            ");
System.out.println("======================================");
System.out.println("==========MENU DE OPCIONES============");
System.out.println("======================================");
System.out.println("1. CARGAR LA MATRIZ M");
System.out.println("2. CARGAR LA MATRIZ N");
System.out.println("3. EJECUTAR LA MULTIPLICACION");
System.out.println("4. MOSTRAR LA MATRIZ RESULTANTE");
System.out.println("5. FIN");
System.out.print("Seleccione una opcion[1-5] :");
opc=tec.nextInt();
switch(opc) {
case 1:
llenar_matriz_M();
break;
case 2:
llenar_matriz_N();
break;
case 3:
multiplica_MxN();
break;
case 4:
imprimir_matriz_R();
break;
case 5:
sw=1;
break;
default:
System.out.println("** ATENCION DIGITO UNA OPCION INVÁLIDA");
}
}
}
static void llenar_matriz_M() {
for (int f=0;f<fm;f++) {
for (int c=0;c<cm;c++) {
System.out.print("Digite el elemento["+f+"]["+c+"] de la matriz M");
M[f][c]=tec.nextInt();
}
}
}
static void llenar_matriz_N() {
for (int f=0;f<cm;f++) {
for (int c=0;c<cn;c++) {
System.out.print("Digite el elemento["+f+"]["+c+"] de la matriz N");
N[f][c]=tec.nextInt();
}
}

}
static void multiplica_MxN() {
for(int i=0;i<fm;i++) {
       for( int j=0;j<cn;j++){
         R[i][j]=0;       
       for (int k=0;k<cm;k++) {
                   R[i][j]=R[i][j]+M[i][k]*N[k][j];        
       }
       }
}
}
static void imprimir_matriz_R() {

for(int i=0;i<fm;i++) {
       for( int j=0;j<cn;j++){
         System.out.print(R[i][j]+"  ");
         }
       System.out.println("");
}

}
}

No hay comentarios:

Trabajo final de excel intermedio

 Se recibe un archivo con la información de la estadistica de venta en formato plano Con estos datos realice lo siguiente: 1. Importe la inf...