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

时间:2015-04-23 05:15:00

标签: java

I need the sum of the variables around 2,2 and then to print it out.

I have no idea on how to do this. Please do help! This is my code so far:

import java.util.*;
import java.io.*; 

public class MatrixSumming
{
private int[][] m = {{5,6},{7,8},{3,4}};   //load in the matrix values

public int sum( int r, int c )
{
    return 0;
}

public String toString()
{
    return "";
}
}

Here is my 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 but couldn't find anything similar to this in matrix.

3 个答案:

答案 0 :(得分:0)

Do a regular search for the required number and let the index vvalues be "curi" and "curj"

Use the following logic Let maxi and maxj be the m and n for (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 the current search key to add
        }
        sum += array[i][j];
      }
   }
}

答案 1 :(得分:0)

This method will get all the surrounding cells values and sum them.

private static int neighboursSum(int[][] grid, int r, int c) {

        int sum = 0;

        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {

                // Make sure that we don't sum the Original Value, we only want it's neighbours
                if (i == 0 && j == 0) {
                    continue;

                } else {

                    int newX = r + i;
                    int newY = c + j;

                    // Make sure that the new Coordinates do not point outside the range of the Array
                    if (newX >= 0 && newX <= grid.length && newY >= 0 && newY <= grid.length) {
                        sum += grid[newX][newY];
                    }
                }
            }
        }
        return sum;
    }

答案 2 :(得分:0)

You have this method public int sum( int r, int c ) where you pass in two ints.

These are the row and column index, and correspond to a position in the matrix. (Or you have to substract 1, if you take row 1 as the first row, because of the zero-indexed arrays in Java)

So if you have a matrix and the values r and c, you will be at position matrix[r][c] (or the other way round).

Imagine standing on a chess board. Now you have 8 fields around you, which you can reach by taking one step. (Or two, if you cannot move diagonally in one step). You might not have fields on one or more sides. You have to check this before computing a sum.

Now, taking a step means adding or substracting 1 to either r or c. Thus you get other indexes around the field you're standing at by addressing matrix[r-1][c] or matrix[r][c+1] etc. To move diagonally, you have to change both r and c (take two steps. e.g. first move one to the left, then one up. you moved to the field diagonally at the upper left of the original field)

You can then, using this knowledge, access the fields around the current field and sum them up.