Consider 2d matrix 0 to 9 variable width and height find the submatrix of sum boundari element

0 votes
asked Feb 24, 2018 by Zimzim

1 Answer

–1 vote
answered Jan 4 by (120 points)
/******************************************************************************

Welcome to GDB Online.

GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,

C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.

Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/

import java.util.Scanner;

public class Main

{

  void avgsubMatrix (int a[][])

  {

    int row = a.length - 2;

    int col = a[0].length - 2;

    int sum, maxSum = 0, subRow = 0, subCol = 0;

    for (int i = 0; i < row; i++)

      {

sum = 0;

for (int j = 0; j < col; j++)

  {

    sum =

      a[i][j] + a[i][j + 1] + a[i][j + 2] + a[i + 1][j] + a[i + 1][j +

   2]

      + a[i + 2][j] + a[i + 2][j + 1] + a[i + 2][j + 2];

    if (maxSum < sum)

      {

maxSum = sum;

subRow = i;

subCol = j;

      }

  }

      }

    for (int i = subRow; i < subRow + 3; i++)

      {

for (int j = subCol; j < subCol + 3; j++)

  {

    System.out.print (a[i][j] + " ");

  }

System.out.println ();

      }

  }

  public static void main (String[]args)

  {

    Main s = new Main ();

    Scanner sc = new Scanner (System.in);

    System.out.print ("Enter rows of Matrix ");

    int row = sc.nextInt ();

    System.out.print ("Enter column of the matrix:");

    int col = sc.nextInt ();

    int Matrix[][] = new int[row][col];

    System.out.println ("Enter  Matrix element between  0 to 9\n");

    for (int i = 0; i < row; i++)

      {

for (int j = 0; j < col; j++)

  {

    Matrix[i][j] = sc.nextInt ();

  }

      }

    s.avgsubMatrix (Matrix);

  }

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...