rootUser

mattrix add subtract sum of row sum of column

May 27th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package javaapplication17;
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.math.*;
  5. public class JavaApplication17 {
  6.     public static int[][] addMatricies(int[][] m1, int[][] m2)
  7.     {
  8.  
  9.     int[][] result = new int[m1.length][m1[0].length];
  10.  
  11.     for (int row=0; row<m1.length; row++) {
  12.         for (int col=0; col<m1[row].length; col++) {
  13.         result[row][col] = m1[row][col] + m2[row][col];
  14.         }
  15.     }
  16.  
  17.     return result;
  18.     }
  19.     public static int[][] minusMatricies(int[][] m1, int[][] m2)
  20.     {
  21.  
  22.     int[][] result = new int[m1.length][m1[0].length];
  23.  
  24.     for (int row=0; row<m1.length; row++) {
  25.         for (int col=0; col<m1[row].length; col++) {
  26.         result[row][col] = m1[row][col] - m2[row][col];
  27.         }
  28.     }
  29.  
  30.     return result;
  31.     }
  32.    
  33.       public static void sumColumn(int[][] m) {
  34.         int r = m.length;
  35.         int c = m[0].length;
  36.         double total = 0;
  37.         for (int row = 0; row < r; row++) {  
  38.           total = 0;
  39.           for (int column = 0; column < c ; column++) {
  40.             total += m[row][column];
  41.  
  42.           }              
  43.          System.out.println("The sum for column "+ row + " is " + total);            
  44.  
  45.         }
  46. }
  47.      
  48.             public static void sumRow(int[][] m) {
  49.         int r = m.length;
  50.         int c = m[0].length;
  51.         double total = 0;
  52.         for (int column = 0; column < c ; column++) {  
  53.           total = 0;
  54.           for (int row = 0; row < r; row++) {
  55.             total += m[row][column];
  56.  
  57.           }              
  58.          System.out.println("The sum for row "+ column+ " is " + total);            
  59.  
  60.         }
  61. }
  62.    
  63.    
  64.    
  65.     public static void main(String[] args) {
  66.         // TODO code application logic here
  67.         Scanner scanner = new Scanner(System.in);
  68.         System.out.print(" enter row : ");
  69.         int row = scanner.nextInt();
  70.         System.out.print(" enter column : ");
  71.         int column = scanner.nextInt();
  72.         int[][] a = new int[row][column];
  73.         int[][] b = new int[row][column];
  74.         int[][] c = new int[row][column];
  75.         System.out.println("enter first mattrix: ");
  76.         for(int i=0;i<row;i++)
  77.         {
  78.             for(int j=0;j<column;j++)
  79.             {
  80.                 a[i][j]=scanner.nextInt();
  81.             }
  82.             System.out.println("");
  83.         }
  84.         System.out.println("enter second mattrix: ");
  85.         for(int i=0;i<row;i++)
  86.         {
  87.             for(int j=0;j<column;j++)
  88.             {
  89.                 b[i][j]=scanner.nextInt();
  90.             }
  91.             System.out.println("");
  92.         }
  93.         System.out.println("Add :");
  94.         c=addMatricies(a,b);
  95.                 for(int i=0;i<row;i++)
  96.         {
  97.             for(int j=0;j<column;j++)
  98.             {
  99.                 System.out.print(c[i][j]+" ");
  100.             }
  101.             System.out.println("");
  102.         }
  103.         System.out.println("Minus");
  104.         c=minusMatricies(a,b);
  105.                         for(int i=0;i<row;i++)
  106.         {
  107.             for(int j=0;j<column;j++)
  108.             {
  109.                 System.out.print(c[i][j]+" ");
  110.             }
  111.             System.out.println("");
  112.         }
  113.                                 System.out.println("Sum of row of  A is : ");
  114.                                 sumRow(a);
  115.         System.out.println("Sum of column of B is : ");
  116.         sumColumn(b);
  117.     }
  118.    
  119. }
Add Comment
Please, Sign In to add comment