Pergunta

I'm writing a program that multiplies 2 matrices using 2D Arrays. Then the results should be printed in a rectangular form. Here's the code:

import java.util.Scanner;
public class MatrixMath   
{



public static void main(String[] args) {
   int m, n, p, q, c, d, k;
   int sum1 = 0;

   Scanner scan = new Scanner(System.in);  

   System.out.println("Please enter the number of rows and columns for the first       matrix."); 
      m = scan.nextInt();
      n = scan.nextInt();

   int first[][] = new int[m][n];

        System.out.println("Enter the elements of first matrix:"); 

        for ( c = 0 ; c < m ; c++ ) 
           for ( d = 0 ; d < n ; d++ )
              first[c][d] = scan.nextInt();

   System.out.println("Please enter the number of rows and columns for the second matrix."); 
      p = scan.nextInt();
      q = scan.nextInt();


     int second[][] = new int[p][q];
     int multiply[][] = new int[m][q];

     System.out.println("Enter the elements of second matrix:"); 

     for ( c = 0 ; c < p ; c++ )   
        for ( d = 0 ; d < q ; d++ )
           second[c][d] = scan.nextInt();


     for ( c = 0 ; c < m ; c++ )  
     {
        for ( d = 0 ; d < q ; d++ )
        {   
           for ( k = 0 ; k < p ; k++ )
           {
           sum1 = sum1 + first[c][k]*second[k][d]; 
           }

           multiply[c][d] = sum1;

           sum1 = 0;
           }
        }

           System.out.println("The product of the two matrices is: ");  

              for ( c = 0 ; c < m ; c++ )
              {
                 for ( d = 0 ; d < q ; d++ )
                 System.out.print(multiply[c][d]+"\t");

                 System.out.print("\n");


           int sum[][] = new int[m][n];  // Sum is calculated.

           for ( c = 0 ; c < m ; c++ )  
                 for ( d = 0 ; d < n ; d++ )




     System.out.println("The sum of the two matrices is: "); 
     for ( c = 0 ; c < m ; c++ )
           {
           for ( d = 0 ; d < n ; d++ )
           System.out.print(sum[c][d]+"\t");

           System.out.println();

   }
  }
 }
}

Output issue after the user enters values:

The product of the two matrices is:
8 8
The sum of the two matrices is:
The sum of the two matrices is:
The sum of the two matrices is:
The sum of the two matrices is:
0 0
0 0

I want the sum printed as a rectangle, as well as the product... but only half the product seems to be printing and something is wrong with the sum output. Help is appreciated. Thanks very much.

Foi útil?

Solução

Personally, I explicitly put braces for all my for-loops and if-statements. You've forgotten several of them, and that's causing your problems. Your outer loops have control variable 'c' - but because you've forgotten to close your braces you're reusing 'c' in an inner loop.

In addition, your indenting is all over the place, so obscuring the issue. In your IDE, find the auto-indent function - it's your friend, and will probably highlight it for you.

So, you should have :

for ( c = 0 ; c < m ; c++ ) {
    for ( d = 0 ; d < q ; d++ ) {
       System.out.print(multiply[c][d]+"\t");
    }

    System.out.print("\n");

} // missed this one

int sum[][] = new int[m][n];  // Sum is calculated.

for ( c = 0 ; c < m ; c++ )   {
    for ( d = 0 ; d < n ; d++ ) {

        System.out.println("The sum of the two matrices is: "); 

etc

Outras dicas

Just try to indent your code properly to avoid confusions.

The complete code is here:

import java.util.Scanner;
public class MatrixMath      
{



 public static void main(String[] args) {
    int m, n, p, q, c, d, k;
    int sum1 = 0;

    Scanner scan = new Scanner(System.in);  

    System.out.println("Please enter the number of rows and columns for the first       matrix."); 
      m = scan.nextInt();
      n = scan.nextInt();

    int first[][] = new int[m][n];

    System.out.println("Enter the elements of first matrix:"); 

    for ( c = 0 ; c < m ; c++ ) 
       for ( d = 0 ; d < n ; d++ )
          first[c][d] = scan.nextInt();

    System.out.println("Please enter the number of rows and columns for the second matrix."); 
    p = scan.nextInt();
    q = scan.nextInt();


     int second[][] = new int[p][q];
     int multiply[][] = new int[m][q];

     System.out.println("Enter the elements of second matrix:"); 

     for ( c = 0 ; c < p ; c++ )   
        for ( d = 0 ; d < q ; d++ )
           second[c][d] = scan.nextInt();


     for ( c = 0 ; c < m ; c++ )  
     {
        for ( d = 0 ; d < q ; d++ )
        {   
           for ( k = 0 ; k < p ; k++ )
           {
               sum1 = sum1 + first[c][k]*second[k][d]; 
           }
           multiply[c][d] = sum1;
           sum1 = 0;
         }
      }

       System.out.println("The product of the two matrices is: ");  

          for ( c = 0 ; c < m ; c++ )
          {
             for ( d = 0 ; d < q ; d++ )
             {
                 System.out.print(multiply[c][d]+"\t");
             }
             System.out.print("\n");
          }
       int sum[][] = new int[m][n];  // Sum is calculated.

       for ( c = 0 ; c < m ; c++ )
       {
           for ( d = 0 ; d < n ; d++ )
           {
               sum[c][d] = first[c][d] + second[c][d];
           }
       }



     System.out.println("The sum of the two matrices is: "); 
     for ( c = 0 ; c < m ; c++ )
     {
           for ( d = 0 ; d < n ; d++ )
           {
               System.out.print(sum[c][d]+"\t");
           }
           System.out.println();
      }
  }
 }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top