Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write two programs that fill and print a matrix of size N x N. Filling a matrix in the regular pattern (top to bottom and left to right) is boring. */
- namespace _01.FillTheMatrix
- {
- using System;
- class FillTheMatrix
- {
- static void Main(string[] args)
- {
- Console.Write("Enter a Positive Integer for size of matrix: ");
- int size = int.Parse(Console.ReadLine());
- Console.WriteLine(@"Please choose a type of the regular pattern to fill:
- 1 --> top to bottom
- 2 --> left to right
- ");
- int numChoice;
- do
- {
- Console.Write("Enter the number of your choice: ");
- } while (!int.TryParse(Console.ReadLine(), out numChoice) || numChoice < 1 || numChoice > 2);
- int[,] matrix = new int[size, size];
- FillTheMatrixByChoicenRegularPattern(matrix, numChoice);
- PrintTheMatrix(matrix);
- }
- private static void FillTheMatrixByChoicenRegularPattern(int[,] matrix, int pattern)
- {
- int n = matrix.GetLength(0);
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (pattern == 2 && col % 2 != 0)
- {
- matrix[row, col] = (n * (col + 1)) - row;
- }
- else
- {
- matrix[row, col] = (n * col) + row + 1;
- }
- }
- }
- }
- private static void PrintTheMatrix(int[,] matrix)
- {
- Console.WriteLine("Your filled matrix is: ");
- Console.WriteLine();
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write("{0,4}", matrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement