Created
August 28, 2013 21:14
-
-
Save bobixaka/6371385 to your computer and use it in GitHub Desktop.
A program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_7_LargestAreaNeighbour | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
//Ask for a size of the matrix | |
Console.Write("Please enter the matrix size: "); | |
string input = Console.ReadLine(); | |
int Size = 0; | |
while (int.TryParse(input, out Size) == false) | |
{ | |
Console.Write("Please enter a valid integer!: "); | |
input = Console.ReadLine(); | |
} | |
//Declare matrix | |
int[,] Matrix = new int[Size, Size]; | |
//Declare all variables: | |
int CurrentRepeats = 0; | |
int MaxRepeats = 0; | |
int StartRow = 0; | |
int StartCol = 0; | |
int RepeatedDigit = 0; | |
int CurrentRow = 0; | |
int CurrentCol = 0; | |
Random RandomDigit = new Random(); | |
//Fill the matrix with random digits (1-9) | |
FillMatrixRandom(Size, Matrix, RandomDigit); | |
//Print the matrix | |
PrintMatrix(Size, Matrix); | |
//Put the whole code in 2 integrated for loops for (rows) ... for (cols) | |
for (StartRow = 0; StartRow < Size; StartRow++) | |
{ | |
for (StartCol = 0; StartCol < Size; StartCol++) | |
{ | |
//Check for OutOfRangeExceptions | |
if ((StartCol+1) < Size) | |
{ | |
//Start (0;0) and look for equal neighbour elements to the right side | |
//If you find equal elements, continue searching to the right side, until you find a different one | |
while (Matrix[StartRow,StartCol] == Matrix[StartRow, StartCol+1]) | |
{ | |
//Save the start position of the first element, so you can go back there | |
//Save the repeated element | |
//Count all found equal elements | |
} | |
} | |
//Check for OutOfRangeExceptions | |
if ((StartRow+1) < Size) | |
{ | |
//Go back on start positon and look for equal neighbour elements down side | |
//If you find equal elements down side, continue searching until you find a different one | |
while (Matrix[StartRow,StartCol] == Matrix[StartRow+1, StartCol]) | |
{ | |
//Save the start position of the first element, so you can go back there. | |
//Save the repeated element | |
//Count all found equal elements | |
} | |
} | |
//If no equal elements found reloop and go to next element in the matrix | |
} | |
} | |
//Print the result -> MaxRepeats, RepeatedDigit | |
} | |
private static void FillMatrixRandom(int Size, int[,] Matrix, Random RandomDigit) | |
{ | |
for (int row = 0; row < Size; row++) | |
{ | |
for (int col = 0; col < Size; col++) | |
{ | |
Matrix[row, col] = RandomDigit.Next(1, 9); | |
} | |
} | |
} | |
private static void PrintMatrix(int Size, int[,] Matrix) | |
{ | |
Console.WriteLine(); | |
for (int row = 0; row < Size; row++) | |
{ | |
for (int col = 0; col < Size; col++) | |
{ | |
Console.Write("{0,3}", Matrix[row, col]); | |
} | |
Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment