java - How do i add the variables around a variable in a matrix -


i need sum of variables around 2,2 , print out.

i have no idea on how this. please help! code far:

import java.util.*; import java.io.*;   public class matrixsumming { private int[][] m = {{5,6},{7,8},{3,4}};   //load in matrix values  public int sum( int r, int c ) {     return 0; }  public string tostring() {     return ""; } } 

here runner

import java.io.file; import java.io.ioexception; import java.util.scanner; import static java.lang.system.*;  public class matrixsummingrunner {     public static void main( string args[] ) throws ioexception     {     //scanner file = new scanner (new file("matsum.dat"));     int[][] mat = {{0, 0, 0, 0, 0, 0, 0},                 {0, 1, 2, 3, 4, 5, 0},                 {0, 6, 7, 8, 9, 0, 0},                 {0, 6, 7, 1, 2, 5, 0},                 {0, 6, 7, 8, 9, 0, 0},                 {0, 5, 4, 3, 2, 1, 0},                 {0, 0, 0, 0, 0, 0, 0}}; } } 

i tried finding couldn't find similar in matrix.

do regular search required number , let index vvalues "curi" , "curj"

use following logic let maxi , maxj m , n (mxn) matrix

sum = 0; for(int i=curi-1;i<curi+3;i++){    for(int j=curj-1;j<curj+3;j++){       if(i>-1 && j>-1 && i<maxi && j<maxj){ // boundary conditions         if(i!=curi && j!=curj){           continue; // skip current search key add         }         sum += array[i][j];       }    } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -