Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Square Matrix multiplication using Java

I'm trying to write a simple square matrix multiplication method using multidimensional arrays.

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();
        }         

    }

}

When I run this program in Netbeans ,I get the following output:

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

The Correct output of this program should be :

2  2
2  2

Can someone tell me what is wrong with this code.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is here:

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

This creates just one array, and makes all of the variables point to it. So, updating the elements of c is also updating the elements of a and b.

Create 3 arrays instead:

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

Ideone demo

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda