aleahim

Multidimensional Arrays_2

Jan 30th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2.  
  3. class FindMaxSumIn
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int m = int.Parse(Console.ReadLine());
  9.  
  10.         int[,] myMatrix = new int[n, m];
  11.         int maxSum = 0;
  12.         int sum = 0;
  13.         int indexRow = 0;
  14.         int indexCol = 0;
  15.         String inputNumber;
  16.         for (int row = 0; row < n; row++)
  17.         {
  18.             for (int col = 0; col < m; col++)
  19.             {
  20.                 Console.Write("matrix[{0},{1}] = ", row, col);
  21.                 inputNumber = Console.ReadLine();
  22.                 myMatrix[row, col] = int.Parse(inputNumber);
  23.                 Console.WriteLine();
  24.             }
  25.         }
  26.         for (int row = 0; row < n - 2; row++)
  27.             for (int col = 0; col < m - 2; col++)
  28.             {
  29.                
  30.                 for (int i = row; i <= row + 2; i++)
  31.                     for (int j = col; j <= col + 2; j++)
  32.                     {
  33.                         sum += myMatrix[i, j];
  34.                     }
  35.                 if (sum > maxSum)
  36.                 {
  37.                     maxSum = sum;
  38.                     indexRow = row;
  39.                     indexCol = col;
  40.                 }
  41.                 sum = 0;
  42.             }
  43.            
  44.        
  45.         Console.WriteLine(maxSum);
  46.         for (int row = indexRow; row <= indexRow + 2; row++)
  47.         {
  48.             for (int col = indexCol; col <= indexCol + 2; col++)
  49.             {
  50.                 Console.Write("{0} ", myMatrix[row, col]);
  51.             }
  52.             Console.WriteLine();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment