Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _02.MaximalSum3x3
- {
- class Program
- {
- static void Main(string[] args)
- {
- //create randomly size matrix
- Random randomNumb = new Random();
- int rowsNumberN = randomNumb.Next(3,15);
- int colsNumberK = randomNumb.Next(3, 15);
- int[,] matrix = new int[rowsNumberN, colsNumberK];
- //create randomly generated matrix with elements till 100;
- matrix = RandomMatrix(matrix,rowsNumberN,colsNumberK);
- //print Matrix
- Print(matrix);
- //check for maximum sum
- CheckMaxSum(matrix,rowsNumberN,colsNumberK);
- }
- private static int[,] RandomMatrix(int[,] matrix, int rowsNumberN, int colsNumberK)
- {
- Random randomNumb = new Random();
- for (int i = 0; i < rowsNumberN; i++)
- {
- for (int j = 0; j < colsNumberK; j++)
- {
- matrix[i, j] = randomNumb.Next(100);
- }
- }
- return matrix;
- }
- private static void CheckMaxSum(int[,] matrix, int rowsNumberN, int colsNumberK)
- {
- int bestSum = int.MinValue;
- int totalSum;
- int bestCellRow = 0;
- int bestCellCol = 0;
- for (int rows = 0; rows < rowsNumberN - 3; rows++)
- {
- for (int cols = 0; cols < colsNumberK - 3; cols++)
- {
- totalSum = SumMatrix(matrix, rows, cols);
- if (totalSum >= bestSum)
- {
- bestSum = totalSum;
- bestCellRow = rows;
- bestCellCol = cols;
- }
- }
- }
- Console.WriteLine("The maximum sum is: {0}", bestSum);
- PrintSmallMatrix(matrix, bestCellRow, bestCellCol);
- }
- private static void PrintSmallMatrix(int[,] matrix, int rows, int cols)
- {
- Console.WriteLine("The coordinate of the first cell is ({0},{1})", rows+1,cols+1);
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- Console.Write("{0,3}", matrix[rows + i, cols + j]);
- }
- Console.WriteLine();
- }
- }
- private static int SumMatrix(int[,] matrix, int rows, int cols)
- {
- int totalSum=0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- totalSum += matrix[rows + i, cols + j];
- }
- }
- return totalSum;
- }
- private static void Print(int[,] matrix)
- {
- int lengthRows = matrix.GetLength(0);
- int lengthCols = matrix.GetLength(1);
- for (int i = 0; i < lengthRows; i++)
- {
- for (int j = 0; j < lengthCols; j++)
- {
- Console.Write("{0,3} ",matrix[i,j]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement