Advertisement
michael_xgrind

Multiplicacao de matriz

Jul 20th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. // multiplicacao de matriz
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Matriz {
  6.     public static void main(String args[]) {
  7.         Scanner leia = new Scanner(System.in);
  8.             int mat1[][] = new int[3][3];
  9.             int mat2[][] = new int[3][3];
  10.             int mat3[][] = new int[3][3];
  11.            
  12.             for(int i=0; i<3; i++) {
  13.                 for(int j=0; j<3; j++) {
  14.                     mat1[i][j] = mat2[i][j] = 1;
  15.                 }
  16.             }      
  17.            
  18.             for(int i=0; i<3; i++) {               
  19.                 for(int j=0; j<3; j++) {
  20.                     int aux = 0;                   
  21.                     mat3[i][j] = 0;
  22.                    
  23.                     /* Multiplica coluna por linha. Ex: c1l1 x c2l2 */
  24.                     for(int x=0; x<3; x++) {
  25.                         aux += mat1[i][x] * mat2[x][j];
  26.                     }                  
  27.                     mat3[i][j] = aux;
  28.                 }
  29.                
  30.             }
  31.            
  32.             System.out.println("\nMatriz 1");
  33.             for(int i=0; i<3; i++) {
  34.                 for(int j=0; j<3; j++) {
  35.                     System.out.print(mat1[i][j] + "\t ");
  36.                 } System.out.print("\n");
  37.             }
  38.            
  39.             System.out.println("\nMatriz 2");
  40.             for(int i=0; i<3; i++) {
  41.                 for(int j=0; j<3; j++) {
  42.                     System.out.print(mat2[i][j] + "\t ");
  43.                 } System.out.print("\n");
  44.             }
  45.            
  46.             System.out.println("\nMatriz 3");
  47.             for(int i=0; i<3; i++) {
  48.                 for(int j=0; j<3; j++) {
  49.                     System.out.print(mat3[i][j] + "\t ");
  50.                 } System.out.print("\n");
  51.             }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement