Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Multiplicación de matrices cuadradas usando Java

Estoy tratando de escribir un método de multiplicación de matriz cuadrada simple usando matrices multidimensionales.

 package matrixmultiplication; import java.util.Scanner; public class Matrixmultiplication { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int [][] a,b,c; int size; System.out.print("Enter the Size of the matrix :"); size = scan.nextInt(); a=b=c=new int[size][size]; System.out.println("Enter the elements of the First matrix"); for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { System.out.print("Enter the element a[" + i +"]["+ j + "] : "); a[i][j] = scan.nextInt(); } } System.out.println("Enter the elements of the Second matrix"); for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { System.out.print("Enter the element b[" + i +"]["+ j + "] : "); b[i][j] = scan.nextInt(); } } System.out.println("The Product of the two matrix is : "); for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { int sum = 0; for(int k=0;k<size;k++) { sum +=(a[i][k] * b[k][j]); } c[i][j] = sum; System.out.print(c[i][j] + "\t"); } System.out.println(); } } }

Cuando ejecuto este programa en Netbeans, obtengo el siguiente resultado:

 Enter the Size of the matrix :2 Enter the elements of the First matrix Enter the element a[0][0] : 1 Enter the element a[0][1] : 1 Enter the element a[1][0] : 1 Enter the element a[1][1] : 1 Enter the elements of the Second matrix Enter the element b[0][0] : 1 Enter the element b[0][1] : 1 Enter the element b[1][0] : 1 Enter the element b[1][1] : 1 The Product of the two matrix is : 2 3 3 10

La salida correcta de este programa debe ser:

 2 2 2 2

¿Puede alguien decirme qué está mal con este código?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

El problema está aquí:

 a=b=c=new int[size][size];

Esto crea solo una matriz y hace que todas las variables apunten a ella. Entonces, actualizar los elementos de c es también actualizar los elementos de a y b .

Cree 3 matrices en su lugar:

 a=new int[size][size]; b=new int[size][size]; c=new int[size][size];

Demostración de ideone

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda