Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //(c) A+ Computer Science
- //www.apluscompsci.com
- //Name -
- import java.util.*;
- import java.io.*;
- public class MatrixSumming2
- {
- public static int sum( int[][] mat, int r, int c )
- {
- int total = 0;
- total += mat[r][c];
- if( r - 1 >= 0 && c - 1 >= 0 ) total += mat[r-1][c-1];
- if( r - 1 >= 0 ) total += mat[r-1][c];
- if( r - 1 >= 0 && c + 1 < mat[0].length ) total += mat[r-1][c+1];
- if( c - 1 >= 0 ) total += mat[r][c-1];
- if( c + 1 < mat[0].length ) total += mat[r][c+1];
- if( r + 1 < mat.length && c - 1 >= 0 ) total += mat[r+1][c-1];
- if( r + 1 < mat.length ) total += mat[r+1][c];
- if( r + 1 < mat.length && c + 1 < mat.length ) total += mat[r+1][c+1];
- return total;
- }
- public static void main(String[] args){
- int[][] m = {{1, 2, 3, 4, 5},
- {6, 7, 8, 9, 0},
- {6, 7, 1, 2, 5},
- {6, 7, 8, 9, 0},
- {5, 4, 3, 2, 1}};
- System.out.println( sum(m, 2, 2) );
- System.out.println( sum(m, 0, 0) );
- System.out.println( sum(m, 4, 3) );
- System.out.println( sum(m, 4, 4) );
- System.out.println( sum(m, 2, 4) );
- System.out.println( sum(m, 1, 3) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement