Advertisement
vlad0

Multidimensional Array Homework - 2

Jan 14th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _02.MaximalSum3x3
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //create randomly size matrix
  13.             Random randomNumb = new Random();
  14.             int rowsNumberN = randomNumb.Next(3,15);
  15.             int colsNumberK = randomNumb.Next(3, 15);
  16.             int[,] matrix = new int[rowsNumberN, colsNumberK];
  17.            
  18.             //create randomly generated matrix with elements till 100;
  19.             matrix = RandomMatrix(matrix,rowsNumberN,colsNumberK);
  20.            
  21.             //print Matrix
  22.             Print(matrix);
  23.             //check for maximum sum
  24.             CheckMaxSum(matrix,rowsNumberN,colsNumberK);
  25.            
  26.         }
  27.  
  28.         private static int[,] RandomMatrix(int[,] matrix, int rowsNumberN, int colsNumberK)
  29.         {
  30.             Random randomNumb = new Random();
  31.             for (int i = 0; i < rowsNumberN; i++)
  32.             {
  33.                 for (int j = 0; j < colsNumberK; j++)
  34.                 {
  35.                     matrix[i, j] = randomNumb.Next(100);
  36.                 }
  37.             }
  38.             return matrix;
  39.         }
  40.  
  41.        
  42.  
  43.         private static void CheckMaxSum(int[,] matrix, int rowsNumberN, int colsNumberK)
  44.         {
  45.             int bestSum = int.MinValue;
  46.             int totalSum;
  47.             int bestCellRow = 0;
  48.             int bestCellCol = 0;
  49.  
  50.             for (int rows = 0; rows < rowsNumberN - 3; rows++)
  51.             {
  52.                 for (int cols = 0; cols < colsNumberK - 3; cols++)
  53.                 {
  54.                     totalSum = SumMatrix(matrix, rows, cols);
  55.  
  56.                     if (totalSum >= bestSum)
  57.                     {
  58.                         bestSum = totalSum;
  59.                         bestCellRow = rows;
  60.                         bestCellCol = cols;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             Console.WriteLine("The maximum sum is: {0}", bestSum);
  66.             PrintSmallMatrix(matrix, bestCellRow, bestCellCol);
  67.         }
  68.  
  69.         private static void PrintSmallMatrix(int[,] matrix, int rows, int cols)
  70.         {
  71.             Console.WriteLine("The coordinate of the first cell is ({0},{1})", rows+1,cols+1);
  72.             for (int i = 0; i < 3; i++)
  73.             {
  74.                 for (int j = 0; j < 3; j++)
  75.                 {
  76.                     Console.Write("{0,3}", matrix[rows + i, cols + j]);
  77.                 }
  78.                 Console.WriteLine();
  79.             }
  80.         }
  81.  
  82.         private static int SumMatrix(int[,] matrix, int rows, int cols)
  83.         {
  84.             int totalSum=0;
  85.             for (int i = 0; i < 3; i++)
  86.             {
  87.                 for (int j = 0; j < 3; j++)
  88.                 {
  89.                     totalSum += matrix[rows + i, cols + j];
  90.                 }
  91.             }
  92.  
  93.             return totalSum;
  94.         }
  95.  
  96.         private static void Print(int[,] matrix)
  97.         {
  98.             int lengthRows = matrix.GetLength(0);
  99.             int lengthCols = matrix.GetLength(1);
  100.  
  101.             for (int i = 0; i < lengthRows; i++)
  102.             {
  103.                 for (int j = 0; j < lengthCols; j++)
  104.                 {
  105.                     Console.Write("{0,3} ",matrix[i,j]);
  106.                 }
  107.                 Console.WriteLine();
  108.             }
  109.            
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement