Advertisement
stak441

MultidimensionalArrays - problem 2

Jan 15th, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _02.MaximalSumOfElements
  7. {
  8.     class MaximalSum
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //=================================================================
  13.             //int n = 4;                 //Use for testing  
  14.             //int m = 5;
  15.  
  16.             //int[,] matrix = {
  17.             //                { 0, 2, 4, 0, 9 },
  18.             //                { 7, 1, 3, 3, 2 },
  19.             //                { 1, 3, 9, 8, 5 },
  20.             //                { 4, 6, 7, 9, 1 }
  21.             //                };
  22.             //=================================================================
  23.  
  24.             Console.WriteLine("Enter number of rows:");
  25.             int n = int.Parse(Console.ReadLine());
  26.             Console.WriteLine("Enter number of columns:");
  27.             int m = int.Parse(Console.ReadLine());
  28.             int[,] matrix = new int[n, m];
  29.  
  30.             for (int i = 0; i < n; i++)
  31.             {
  32.                 for (int j = 0; j < m; j++)
  33.                 {
  34.                     Console.Write("number[{0},{1}] = ", i, j);
  35.                     matrix[i, j] = int.Parse(Console.ReadLine());
  36.                 }
  37.             }
  38.             //=================================================================
  39.  
  40.             int bestSum = int.MinValue;
  41.             int bestStartRow = 0;
  42.             int bestStartColumn = 0;
  43.  
  44.             for (int row = 0; row <= n-3; row++)
  45.             {
  46.                 for (int col = 0; col <= m-3; col++)
  47.                 {
  48.                     int tempSum = 0;                          
  49.                     tempSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2]
  50.                             + matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2]
  51.                             + matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  52.                     //Can also be done with another pair of nested loops, but since we have a fixed size, it is not needed
  53.  
  54.                     if (tempSum > bestSum)
  55.                     {
  56.                         bestSum = tempSum;
  57.                         bestStartRow = row;
  58.                         bestStartColumn = col;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             Console.WriteLine("Best sum: {0}, coordinates of start point: ({1}, {2})", bestSum, bestStartRow, bestStartColumn);
  64.             for (int i = bestStartRow; i < bestStartRow + 3; i++)
  65.             {
  66.                 for (int j = bestStartColumn; j < bestStartColumn + 3; j++)
  67.                 {
  68.                     Console.Write("{0,-5}", matrix[i, j]);
  69.                 }
  70.                 Console.WriteLine();
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement